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
64
#include <stdlib.h>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#define LL long long
#define ULL unsigned long long
#define U unsigned
#define FOR(x,z) for(int x=0;x<(z);++x)
#define DS(i) fprintf(stderr, "DEBUG: %s\n",i);
#define DI(i) fprintf(stderr, "DEBUG: %d\n",i);
#define DF(i) fprintf(stderr, "DEBUG: %f\n",i);
using namespace std;
U n, m, tmp;
ULL d, b;
vector<U> szybkosc;
void wczytaj()
{
    cin >> n >> m;
    szybkosc.reserve(m);
    FOR(i,n)
    {
        cin >> tmp;
        szybkosc.push_back(tmp);
    }
    sort(szybkosc.begin(), szybkosc.end(), greater<ULL>());
}
vector<ULL> wys;
ULL wynik=0;
ULL ostatnie_koszenie =0 ;
ULL delta =0;
void wykonaj()
{
    wys.assign(m, 0);
    for(;m>0; --m, wynik=0)
    {
        cin >> d >> b;
        delta = d- ostatnie_koszenie;
        ostatnie_koszenie = d;

        transform(szybkosc.begin(), szybkosc.end(), wys.begin(), wys.begin(),  [](const ULL& a, const ULL& b){ return b+a*delta;});

        std::vector<ULL>::iterator it = wys.begin();
        for(;it != wys.end();++it)
        {
            if(*it <= b)
                break;
            wynik+=*it-b;
            (*it)=b;
        }
        cout << wynik << endl;
    }
}
int main()
{
    wczytaj();

    wykonaj();

    return 0;
}