#include <stdio.h>
 
int main(float main) {
    int p[10], at[10], bt[10];
    int i, j, temp, n;
    float awt = 0, atat = 0;
int over=0,time=0,count,sum_wait=0,sum_tat=0,start;
    printf("Enter number of processes:\n");
    scanf("%d", &n);
 for (i = 0; i < n; i++) {
        p[i] = i + 1;
    }
 
    printf("Enter arrival time of each process:\n");
    for (i = 0; i < n; i++) {
        scanf("%d", &at[i]);
    }
 
    printf("Enter burst time of each process:\n");
    for (i = 0; i < n; i++) {
        scanf("%d", &bt[i]);
    }
 
    // Sort processes by arrival time using bubble sort
    for (i = 0; i < n - 1; i++) {
        for (j = i+1; j < n; j++) {
            if (at[i] > at[j]) {
                // Swap arrival times
                temp = at[i];
                at[i] = at[j];
                at[j] = temp;
 
                // Swap burst times
               temp = bt[i];
                bt[i] = bt[j];
                bt[j] = temp;
 
                // Swap process IDs
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
    printf("\nP\tAT\tBT\tstart time \t end time\t WT\t TAT\n");
    while(over<n){
    	count=0;
    	for(i=over;i<n;i++){
    		if(at[i]<=time)
    		count++;
    		else
    		break;
    	}
    	if(count>1){
    		for(i=over;i<over+count-1;i++){
    			for(j=i+1;j<over+count;j++){
    			if (bt[i] > bt[j]) {
                // Swap arrival times
                temp = at[i];
                at[i] = at[j];
                at[j] = temp;
 
                // Swap burst times
               temp = bt[i];
                bt[i] = bt[j];
                bt[j] = temp;
 
                // Swap process IDs
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }	
    			}
    		}
    	start=time;
    	time+=bt[over];
    	printf("\np[%d]\t %d\t %d\t %d\t %d\t %d\t %d",p[over],at[over],bt[over],start,time,time-at[over]-bt[over],time-at[over]);
    	sum_wait+=time-at[over]-bt[over];
    	sum_tat+=time-at[over];
    	over++;
    }
    atat=(float)sum_tat/n;
    awt=(float)sum_wait/n;
 
    printf("\nAverage Turnaround Time = %.2f", atat);
    printf("\nAverage Waiting Time = %.2f\n", awt);
 
    return 0;
}