-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2188.cpp
More file actions
57 lines (43 loc) · 1.19 KB
/
2188.cpp
File metadata and controls
57 lines (43 loc) · 1.19 KB
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
// https://acm.timus.ru/problem.aspx?space=1&num=2188
// prefix sums + backward DP for the minimum safe lead + monotonicity + binary search over answers (per query)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vll a(n + 1), b(n + 1), g(n + 1);
rep(i, 1, n) cin >> a[i];
rep(i, 1, n) cin >> b[i];
rep(i, 1, n) g[i] = a[i] - b[i];
vll C(n + 1, 0), M(n + 1, 0);
rep(i, 1, n)
{
C[i] = C[i - 1] + g[i];
M[i] = max(M[i - 1], C[i]);
}
vll need(n + 1, 1);
need[n] = 1;
for (int i = n - 1; i >= 0; --i) need[i] = max(1LL, need[i + 1] - g[i + 1]);
vll best(n + 1, 0);
rep(t, 1, n)
{
ll T = C[t] - need[t];
ll dmax = (T >= M[t - 1] ? T : -1LL);
best[t] = max(best[t - 1], dmax);
}
while (q--)
{
ll D;
cin >> D;
auto it = lower_bound(best.begin() + 1, best.end(), D);
if (it == best.end()) cout << -1 << '\n';
else cout << (int)(it - best.begin()) << '\n';
}
return 0;
}