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
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cassert>
#include <ctime>
using namespace std;
const vector<int64_t> NO;

vector<int64_t> solveSlow(vector<int64_t> times, vector<int> oven_times) {
    int n = times.size(), m = oven_times.size();
    vector<int64_t> ans(m, 0);

    for(int oven = 0; oven < m; ++oven) {
        int64_t now = 0;

        for(int i = 0; i < n; ++i) {
            ans[oven] -= times[i];
            now = max(times[i], now + oven_times[oven]);
            ans[oven] += now;
        }
    }

    return ans;
};

struct Chain {
    int first, last, many;
    int64_t start_time;
    
    Chain() {
    };
        
    Chain(int first, int last, int64_t start_time) {
        this->first = first;
        this->last = last;
        this->start_time = start_time;
        this->many = last - first + 1;
    }
    
    int64_t getDelta() const {
        return 1LL * (many - 1) * many / 2;
    }

    int64_t getShare(int time) const {
        return 1LL * (many - 1) * many / 2 * time + start_time * many;
    }

    pair<int64_t, int64_t> getRange(int time) const {
        return make_pair(start_time - time, start_time + 1LL * (many - 1) * time);
    };    

    int64_t getCollisionTime(const Chain &right, int time) const {
        int64_t diff = right.getRange(time).first - this->getRange(time).second;
        if(diff <= 0) {
            return diff;
        } else {
            return (diff + many - 1) / many;
        }
    };
};

vector<int64_t> solveFast(vector<int64_t> times, vector<int> oven_times) {
    vector<int64_t> ans(1e6 + 5, 0);
    
    int maxx = 0;
    for(auto elem : oven_times)
        maxx = max(elem, maxx);

    times.insert(times.begin(), 0);
    int n = times.size();
    int m = oven_times.size();

    vector<int> dad(n, 0), size(n, 0);
    for(int i = 0; i < n; ++i) {
        dad[i] = i;
        size[i] = 1;
    }
        
    int64_t current = 0, delta = 0;

    auto f = [&] (int x) {
        int root = x;
        while(root != dad[root]) {
            root = dad[root];
        }
        while(x != root) {
            int temp = dad[x];
            dad[x] = root;
            x = temp;
        }
        return root;
    };

    auto unite = [&] (int a, int b) {
        int fa = f(a), fb = f(b);
        if(fa == fb)
            return;

        if(size[fb] > size[fb]) {
            dad[fa] = fb;
        } else {
            dad[fb] = fa;
        }

        if(size[fb] == size[fa]) {
            size[fa] += 1;
        }
    };

    auto merge = [&] (Chain &a, Chain &b, int time) {
        if(time > 0) 
            current -= a.getShare(time - 1) + b.getShare(time - 1);
        delta -= a.getDelta() + b.getDelta();

        Chain result(a.first, b.last, times[a.first]);
    
        if(time > 0) 
            current += result.getShare(time - 1);
        delta += result.getDelta();
        
        unite(f(a.first), f(b.first));
        return result;
    };

    vector<Chain> chain(n);
    vector<vector<int>> events(1e6 + 5, vector<int> ());
	
    auto nextChain = [&] (int who) -> int {
        int nxt = chain[f(who)].last + 1;
        if(nxt >= n) {
            return nxt;
        } else {
            return f(nxt);
        }
    };

    auto checkForCollision = [&] (int who, int time, bool add) -> int {
        int nxt = nextChain(f(who));
        if(nxt >= n)
            return 1;
        
        int64_t collision_time = chain[f(who)].getCollisionTime(chain[f(nxt)], time);
        if(collision_time <= 0) {
            return -1;
        } else {
            if(add and collision_time + time <= 1e6) {
                events[collision_time + time].push_back(f(who));
            }
            return 1;
        }
    };
        
    int64_t sub = 0;
    for(int i = n - 1; i >= 0; --i) {
        sub += times[i];
        chain[i] = Chain(i, i, times[i]);
        current += chain[i].getShare(0);
        delta += chain[i].getDelta();
        if(i + 1 < n and checkForCollision(i, 0, false) == -1) {
            auto temp = merge(chain[f(i)], chain[nextChain(f(i))], 0);
            chain[f(i)] = temp;
        }
    }

    for(int i = 0; i < n; ++i) {
        if(f(i) == i) {
            checkForCollision(i, 0, true);
        }
    }
    
    auto showSegs = [&] () {
        for(int i = 0; i < n; ++i)
            if(f(i) == i) {
                cerr << chain[i].first << " " << chain[i].last << "\n";
            }
        cerr << "###\n";
    };
        
    int sum = 0;

    for(int now = 1; now <= maxx; now += 1) {
        sort(events[now].begin(), events[now].end());
        auto it = unique(events[now].begin(), events[now].end());
        events[now].resize(distance(events[now].begin(), it));

        sum += events[now].size();
        for(auto who : events[now]) {
            while(true) { 
                int flag = checkForCollision(f(who), now, false);
                if(flag < 0) {
                    auto temp = merge(chain[f(who)], chain[nextChain(f(who))], now);
                    chain[f(who)] = temp;
                } else {
                    checkForCollision(f(who), now, true);
                    break;
                }
            }
        }
        current += delta;
        ans[now] = current - sub;
    }
    
    cerr << sum << "\n";
    vector<int64_t> sol(m, 0);
    for(int i = 0; i < m; ++i)
        sol[i] = ans[oven_times[i]];
    return sol;
}

int main() {
    ios_base :: sync_with_stdio(false);
    int n, m; cin >> n >> m;
    
    vector<int64_t> times(n, 0);
    vector<int> oven_times(m, 0);

    for(int i = 0; i < n; ++i) {
        cin >> times[i];
    }

    for(int i = 0; i < m; ++i) {
        cin >> oven_times[i];
    }

    auto sol = solveFast(times, oven_times);
    for(auto val : sol) {
        cout << val << "\n";
    }
}