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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,n) for (decltype(a) i = (a), i##__ = (n); i <= i##__; ++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define FORD(i,a,n) for (decltype(n) i = (a), i##__ = (n); i >= i##__; --i)
#define REPD(i,n) FORD(i,(n)-1,0)
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
#define EB emplace_back
#define ST first
#define ND second
#define OS ostream
#define OO(A) template<class... T> OS& operator<<(OS& os, const A<T...>& x) { return __o(os, ALL(x)); }
#define OD(...) OS& operator<<(OS &os, const __VA_ARGS__ &x)
#define SZ(x) ((int)x.size())
#define RS resize
#define V vector
#define nl '\n'

typedef long long LL;
typedef pair<int, int> PII;
typedef V<int> VI;
typedef V<VI> VVI;
typedef V<PII> VPII;
typedef V<VPII> VVPII;
typedef V<bool> VB;
typedef V<VB> VVB;

template<class I> OS& __o(OS&, I, I);
template<class T, size_t N> OD(array<T, N>) { return __o(os, ALL(x)); }
OO(vector) OO(deque) OO(set) OO(multiset) OO(map) OO(multimap)
template<class A, class B> OD(pair<A, B>) {
    return os << "(" << x.ST << ", " << x.ND << ")";
}
template<class I> OS& __o(OS& os, I a, I b) {
    os << "{ ";
    for (; a != b;)
        os << *a++, os << " ";
    return os << "}";
}
template<class I> OS& __d(OS& os, I a, I b) {
    os << "{\n";
    for (I c = a; a != b; ++a)
        os << "  " << distance(c, a) << ": " << *a << endl;
    return os << "}";
}
template<class... T> void __e(T&&... a) {
    int t[] = {(cerr << forward<T>(a), 0)...}; (void)t;
    cerr << endl;
}

template<class A, class B> inline void mini(A& a, B&& b) { if (b < a) a = b; }
template<class A, class B> inline void maxi(A& a, B&& b) { if (b > a) a = b; }

inline int pow2(int n) { return sizeof(int) * 8 - __builtin_clz(n); }

#ifdef DEBUG
# define D(...) __VA_ARGS__
#else
# define D(...)
#endif

#define LOG(x) D(cerr << #x ": " << x << "  ")
#define LOGN(x) D(LOG(x) << endl)
#define DUMP(x) D(cerr << #x ": ", __d(cerr, ALL(x)) << endl)
#define E(...) D(__e(__VA_ARGS__))

//end of templates

int n;

LL ceil_divide(LL a, LL b) {
    if(a % b == 0)
        return a / b;
    else
        return a / b + 1;
}

LL newton2(LL x) {
    return x * (x-1) / 2;
}

VVI predictions;

void rm_prediction(int index);
void add_prediction(int index, LL d);

struct Leader;

Leader& find(int index);
OD(Leader);

int current_time = 0;
LL sum_lateness = 0;
LL to_add_in_next_d = 0;


struct Leader {
    int index;
    LL begin, end;
    int nr_dudes = 1;
    int last_update;
    pair<int, int> where_predict = {-1, -1};

    void lengthen(int t) {
        end += LL(nr_dudes) * (t - last_update);
        last_update = t;
    }

    Leader& operator+=(Leader &l) {
        E("adding ", l.index, " to ", index);
        lengthen(current_time);
        l.lengthen(current_time);
        E("  lengthened");
        E("  ", l);
        E("  ", *this);

        sum_lateness += LL(l.end - begin) * nr_dudes;

        end += l.end - begin;
        begin = l.begin;

        to_add_in_next_d -= newton2(l.nr_dudes);
        to_add_in_next_d -= newton2(nr_dudes);

        nr_dudes += l.nr_dudes;
        l.nr_dudes = 0;
        D(
            l.begin = l.end = l.last_update = -1;
        )
        to_add_in_next_d += newton2(nr_dudes);

        rm_prediction(l.index);
        rm_prediction(index);

        l.index = index;
        E("  rm predictions");
        if(index == n) {
            E("  end");
            return *this;
        }
        LL delta_d = max(0ll, find(index + 1).begin - end);
        add_prediction(index, last_update + ceil_divide(delta_d, nr_dudes));
        E("  end");
        return *this;
    }
};
OD(Leader) {
    if(!x.nr_dudes)
         return os << '-';
    return os << "{ " << x.index << ": " << make_pair(x.begin, x.end) << ", #" << x.nr_dudes << " (" << x.last_update << "), " << x.where_predict << " }";
}

V<Leader> leader;

Leader& find(int index) {
    if(index == leader[index].index)
        return leader[index];
    leader[index].index = find(leader[index].index).index;
    return find(leader[index].index);
}

void add_prediction(int index, LL d) {
     E("+ add pred for ", index, ": ", d);
    if(d >= SZ(predictions))
        return;
    predictions[d].EB(index);
    leader[index].where_predict = {d, SZ(predictions[d]) - 1};
    // E("- add pred");
}

void rm_prediction(int index) {
    auto &where = leader[index].where_predict;
    if(where.ST == -1)
        return;
    // E("+ rm pred ", index);
    if(where.ND != SZ(predictions[where.ST]) - 1) {
        swap(predictions[where.ST][where.ND], predictions[where.ST].back());
        swap(where.ND, leader[predictions[where.ST][where.ND]].where_predict.ND);
    }
    predictions[where.ST].pop_back();
    where = {-1, -1};
    // E("- rm pred");
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int m;
    cin >> n >> m;
    V<LL> t(n);
    for(auto &l : t)
        cin >> l;
    VI requests(m);
    int max_d = 0;
    for(int &d : requests) {
        cin >> d;
        maxi(max_d, d);
    }
    LOG(n); LOG(m); DUMP(t); DUMP(requests);

    leader.RS(n + 1);
    REP(i, n) {
        auto &l = leader[i + 1];
        l.begin = l.end = t[i];
        l.index = i + 1;
    }
    DUMP(leader);

    predictions.RS(max_d + 1);
    REP(i, n)
        add_prediction(i, leader[i+1].end - leader[i].end);
    V<LL> answer(max_d + 1);

    REP(d, max_d + 1) {
        sum_lateness += to_add_in_next_d;
        LOG(d);
        LOG(sum_lateness);
        DUMP(predictions);
        // DUMP(leader);

        while(SZ(predictions[d])) {
            VI pred = predictions[d];
            for(int pred_index : pred) {
                if(leader[pred_index].nr_dudes)
                    find(pred_index + 1) += leader[pred_index];
                DUMP(leader);
            }
        }

        ++current_time;
        answer[d] = sum_lateness;
    }
    DUMP(answer);

    for(int d : requests)
        cout << answer[d] << nl;
    return 0;
}