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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <cassert>

using namespace std;

typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define FOR(x, b, e) for (int x = b; x <= (e); ++x)
#define FORD(x, b, e) for (int x = b; x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define EB emplace_back

#define INF 1000000001

int n, m;

map<LL, int> T;
LL sum = 0;
LL delays = 0;

using PLLI = pair<LL, int>;

priority_queue<PLLI, std::vector<PLLI>, std::greater<PLLI>> Q;

LL Count(LL d)
{
    return (d - 1) * d / 2;
}

LL SumTime(int d, int dp)
{
    sum += (d - dp) * delays;
    while (!Q.empty()) {
        if (Q.top().ST >= d) break;

        auto p = Q.top();
        Q.pop();

        auto it = T.find(p.ND);
        if (it == T.end()) continue;

        if (it == T.begin()) {
            LL dt = d - it->ST;
            assert(dt > 0);
            //printf("T.begin adding: %lld\n", it->ND * dt);
            sum += it->ND * dt;
            auto prev = it++;
            if (it != T.end()) {
                Q.emplace((it->ST - d) / prev->ND, it->ST);
            }

            Q.emplace(d, d);
            int cnt = prev->ND;
            T.erase(prev);
            T[d] = cnt;
        } else {
            auto next = it--;
            LL dt = it->ST + LL(d) * it->ND - next->ST;
            //printf("d: %d it: %lld %d next: %lld %d\n",  d, it->ST, it->ND, next->ST, next->ND);
            assert(dt > 0);
            //printf("else adding: %lld\n", next->ND * dt);
            sum += next->ND * dt;
            LL del = delays;
            delays -= Count(it->ND);
            delays -= Count(next->ND);
            delays += Count(it->ND + next->ND);
            assert(delays >= del);

            it->ND += next->ND;
            T.erase(next);

            auto prev = it++;
            if (it != T.end()) {
                Q.emplace((it->ST - prev->ST) / prev->ND, it->ST);
            }
        }
    }

    return sum;
}

int main()
{
    LL ti;
    VI d;

    cin >> n >> m;
    REP(i, n) {
        cin >> ti;
        auto it = T.find(ti);
        if (it == T.end()) {
            if (T.empty()) {
                Q.emplace(ti, ti);
            } else {
                Q.emplace((ti - T.rbegin()->ST) / T.rbegin()->ND, ti);
            }

            T[ti] = 1;
        } else {
            delays += T[ti];
            ++T[ti];
        }
    }

    vector<PII> D(m);
    REP(i, m) {
        cin >> D[i].ST;
        D[i].ND = i;
    }

    sort(ALL(D));

    vector<LL> ans(m, 0);
    int dprev = 0;
    REP(i, m) {
        //printf("i: %d D[i]: %d %d\n", i, D[i].ST, D[i].ND);
        ans[D[i].ND] = SumTime(D[i].ST, dprev);
        dprev = D[i].ST;
    }

    REP(i, m) {
        cout << ans[i] << "\n";
    }

    return 0;
}