1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

unsigned long get_max_time(unsigned long time, vector<unsigned long> customers);

int main() {


    unsigned long n;
    unsigned long m;
    unsigned long t;

    cin >> n >> m;

    vector<unsigned long> cust(n);

    for (unsigned long i=0; i<n; i++) {
        cin >> t;
        cust[i] = t;
    }
    unsigned int res[m];
    for (unsigned long i=0; i<m; i++) {
        cin >> t;
        res[i] = get_max_time(t,cust);
    }
    for (unsigned long i=0; i<m; i++)
        cout << res[i] <<endl;

    return 0;
}

unsigned long get_max_time(unsigned long cost, vector<unsigned long> customers){
    sort(customers.begin(), customers.end());
    unsigned long total_wait = 0;
    unsigned long cust_wait = 0;
    unsigned int elapsed = 0;
    long temp = 0;

    for(int i=0; i<customers.size(); i++){
        cust_wait = 0;
        //first check if customer had to wait before his job was started and add it to wait time
        temp = elapsed - customers[i];
        if(temp > 0) {
            cust_wait += temp + cost;
            elapsed += cost;
        }
        else {
            temp = cost + elapsed - customers[i];
            if (temp > 0) {
                elapsed += cost;
                cust_wait += cust_wait + temp;
            } else {
                elapsed = customers[i];
            }
        }
        total_wait += cust_wait;
    }

    return total_wait;
}