#include<bits/stdc++.h>
using namespace std;
int n, q;
long long pos[200005];
long long x, sum;
long long lmax, rmax;
vector< pair<long long, pair<long long,long long> > > a, b;
int main() {
ios::sync_with_stdio(false);
cin >> n >> q;
for(int i=1; i<=n; i++) cin >> pos[i];
sum = 0;
lmax = rmax = 0;
while(q--) {
cin >> x;
sum += x;
if(x < 0 && sum < lmax) {
lmax = sum;
a.push_back({-lmax+rmax, {lmax, rmax} });
} else if(x > 0 && sum > rmax) {
rmax = sum;
b.push_back({-lmax+rmax, {lmax, rmax} });
}
}
for(int i=1; i<=n; i++) {
long long ans = 0;
if(i == 1) {
if(a.size() > 0)
ans += -a[a.size()-1].second.first;
} else {
long long gap = pos[i] - pos[i-1];
int p = lower_bound(a.begin(), a.end(), make_pair(gap, make_pair(0LL,0LL))) - a.begin();
long long tmp = 0;
if(p > 0) tmp = max(tmp, -a[p-1].second.first);
if(p < a.size()) tmp = max(tmp, gap-a[p].second.second);
ans += tmp;
}
if(i == n) {
if(b.size() > 0)
ans += b[b.size()-1].second.second;
} else {
long long gap = pos[i+1] - pos[i];
int p = lower_bound(b.begin(), b.end(), make_pair(gap, make_pair(0LL,0LL))) - b.begin();
long long tmp = 0;
if(p > 0) tmp = max(tmp, b[p-1].second.second);
if(p < b.size()) tmp = max(tmp, gap+b[p].second.first);
ans += tmp;
}
cout << ans << "\n";
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:37:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if(p < a.size()) tmp = max(tmp, gap-a[p].second.second);
| ~~^~~~~~~~~~
Main.cpp:49:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if(p < b.size()) tmp = max(tmp, gap+b[p].second.first);
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
2 ms |
428 KB |
Output is correct |
16 |
Correct |
1 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
2 ms |
428 KB |
Output is correct |
16 |
Correct |
1 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
42 ms |
7276 KB |
Output is correct |
21 |
Correct |
30 ms |
7056 KB |
Output is correct |
22 |
Correct |
29 ms |
6800 KB |
Output is correct |
23 |
Correct |
26 ms |
6736 KB |
Output is correct |
24 |
Correct |
34 ms |
7108 KB |
Output is correct |
25 |
Correct |
143 ms |
11992 KB |
Output is correct |
26 |
Correct |
125 ms |
11776 KB |
Output is correct |
27 |
Correct |
134 ms |
11168 KB |
Output is correct |
28 |
Correct |
130 ms |
11148 KB |
Output is correct |
29 |
Correct |
110 ms |
8572 KB |
Output is correct |
30 |
Correct |
105 ms |
5796 KB |
Output is correct |
31 |
Correct |
60 ms |
5240 KB |
Output is correct |
32 |
Correct |
50 ms |
5336 KB |
Output is correct |
33 |
Correct |
11 ms |
1492 KB |
Output is correct |
34 |
Correct |
94 ms |
7736 KB |
Output is correct |
35 |
Correct |
103 ms |
7512 KB |
Output is correct |
36 |
Correct |
121 ms |
11652 KB |
Output is correct |
37 |
Correct |
120 ms |
10544 KB |
Output is correct |
38 |
Correct |
127 ms |
11144 KB |
Output is correct |
39 |
Correct |
126 ms |
11308 KB |
Output is correct |
40 |
Correct |
81 ms |
11400 KB |
Output is correct |
41 |
Correct |
39 ms |
8580 KB |
Output is correct |
42 |
Correct |
52 ms |
5336 KB |
Output is correct |
43 |
Correct |
83 ms |
13688 KB |
Output is correct |
44 |
Correct |
32 ms |
7680 KB |
Output is correct |
45 |
Correct |
72 ms |
11352 KB |
Output is correct |
46 |
Correct |
84 ms |
13600 KB |
Output is correct |
47 |
Correct |
90 ms |
10712 KB |
Output is correct |
48 |
Correct |
84 ms |
13868 KB |
Output is correct |