---
# System prepended metadata

title: 50055. Waiting Time at Supermarket
tags: [C Programming, judgegirl]

---

```clike=
#include<stdio.h>
#define M 1000001
#define N 1028
// #define debug
int max(int a, int b){
    return a * (a > b) + b * (a <= b);
}
int main(){
    int n, m;//n cashiers, m costumers
    scanf("%d%d", &n, &m);
    int arrive, spend;
    int cashiers[N] = {0};
    int wait = 0;
    for(int i = 0; i < m; i++){
        scanf("%d%d", &arrive, &spend);
        int mincash = 0;
        for(int j = 1; j < n; j++){
            if(cashiers[mincash] > cashiers[j])
                mincash = j;
        }
        int dt = cashiers[mincash] - arrive;
        #ifdef debug
        printf("mincash = %d, dt = %d\n", mincash, dt);
        #endif
        wait += (dt > 0) * (dt);
        cashiers[mincash] = (max(cashiers[mincash], arrive) + spend);
    }
    printf("%d\n", wait);
}
```