#include<bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int MAXN = 262144;
int N;
long long arr[MAXN];
int idx[2*MAXN];
void setv(int a, long long v)
{
arr[a] = v;
idx[a+MAXN] = a;
a += MAXN;
while((a=a/2))
{
if(arr[idx[2*a]] < arr[idx[2*a+1]])
idx[a] = idx[2*a+1];
else
idx[a] = idx[2*a];
}
}
int geti()
{
return idx[1];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int N; cin >> N;
set<int> S;
setv(0, -INF); S.insert(0);
for(int i=1; i<=N; ++i)
{
long long t; cin >> t;
setv(i, t); S.insert(i);
}
setv(N+1, -INF); S.insert(N+1);
auto query = [&]()
{
int idx = geti(); long long retv = arr[idx];
auto it = S.find(idx);
auto it2 = it; --it2; int pidx = *it2;
auto it3 = it; ++it3; int nidx = *it3;
S.erase(it2); S.erase(it3);
long long newval = arr[pidx]+arr[nidx] - arr[idx];
setv(pidx, -INF);
setv(nidx, -INF);
setv(idx, newval);
return retv;
};
long long ans = 0;
for(int i=0; i<(N+1)/2; ++i)
{
long long t; cin >> t;
ans += query();
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
504 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
6 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
504 KB |
Output is correct |
17 |
Correct |
6 ms |
508 KB |
Output is correct |
18 |
Correct |
6 ms |
504 KB |
Output is correct |
19 |
Correct |
6 ms |
504 KB |
Output is correct |
20 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
504 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
7 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
6 ms |
504 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
6 ms |
504 KB |
Output is correct |
17 |
Correct |
6 ms |
508 KB |
Output is correct |
18 |
Correct |
6 ms |
504 KB |
Output is correct |
19 |
Correct |
6 ms |
504 KB |
Output is correct |
20 |
Correct |
6 ms |
504 KB |
Output is correct |
21 |
Correct |
257 ms |
14624 KB |
Output is correct |
22 |
Correct |
261 ms |
14456 KB |
Output is correct |
23 |
Correct |
248 ms |
14456 KB |
Output is correct |
24 |
Correct |
171 ms |
14588 KB |
Output is correct |
25 |
Correct |
174 ms |
14456 KB |
Output is correct |
26 |
Correct |
176 ms |
14456 KB |
Output is correct |
27 |
Correct |
171 ms |
14584 KB |
Output is correct |
28 |
Correct |
177 ms |
14456 KB |
Output is correct |
29 |
Correct |
171 ms |
14456 KB |
Output is correct |
30 |
Correct |
183 ms |
14512 KB |
Output is correct |
31 |
Correct |
179 ms |
14456 KB |
Output is correct |
32 |
Correct |
174 ms |
14456 KB |
Output is correct |
33 |
Correct |
210 ms |
14496 KB |
Output is correct |
34 |
Correct |
205 ms |
14456 KB |
Output is correct |
35 |
Correct |
201 ms |
14456 KB |
Output is correct |
36 |
Correct |
255 ms |
14456 KB |
Output is correct |
37 |
Correct |
247 ms |
14460 KB |
Output is correct |
38 |
Correct |
261 ms |
14456 KB |
Output is correct |
39 |
Correct |
169 ms |
14456 KB |
Output is correct |
40 |
Correct |
171 ms |
14456 KB |
Output is correct |
41 |
Correct |
170 ms |
14456 KB |
Output is correct |
42 |
Correct |
173 ms |
14456 KB |
Output is correct |
43 |
Correct |
177 ms |
14456 KB |
Output is correct |
44 |
Correct |
172 ms |
14456 KB |
Output is correct |
45 |
Correct |
177 ms |
14456 KB |
Output is correct |
46 |
Correct |
178 ms |
14456 KB |
Output is correct |
47 |
Correct |
182 ms |
14456 KB |
Output is correct |
48 |
Correct |
203 ms |
14456 KB |
Output is correct |
49 |
Correct |
205 ms |
14456 KB |
Output is correct |
50 |
Correct |
201 ms |
14384 KB |
Output is correct |