#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
const ll N = 2e5 + 100;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll block = 350;
ll a[N], prv[N], nxt[N], del[N], ans[N];
ll n;
struct cmp{
bool operator()(const pll &a, const pll &b){
return a.S < b.S;
}
};
void to_thic_cau(){
cin >> n;
priority_queue<pll, vector<pll>, cmp>q;
for(int i = 1; i <= n;i++){
cin >> a[i];
prv[i] = i - 1, nxt[i] = i + 1;
q.push({i, a[i]});
}
prv[n+1] = n, nxt[0] = 1, a[0] = a[n+1] = -inf;
ll cur = (n + 1) / 2, cnt = 0, res = 0;
while(cnt < cur){
if(!q.size()) break;
ll i = q.top().F, val = q.top().S; q.pop();
if(!del[i] && i > 0 && i <= n){
del[prv[i]] = del[nxt[i]] = 1;
res += val;
a[i] = a[prv[i]] + a[nxt[i]] - val;
q.push({i, a[i]});
nxt[i] = nxt[nxt[i]]; prv[i] = prv[prv[i]];
nxt[prv[i]] = i;
prv[nxt[i]] = i;
cnt++;
}
ans[cnt] = max(ans[cnt], res);
}
for(int i = 1; i <= cur;i++) cout << ans[i] << ' ';
cout << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc = 1;
//cin >> tc;
while(tc--) to_thic_cau();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4688 KB |
Output is correct |
9 |
Correct |
2 ms |
4656 KB |
Output is correct |
10 |
Correct |
2 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
2 ms |
4688 KB |
Output is correct |
17 |
Correct |
2 ms |
4688 KB |
Output is correct |
18 |
Correct |
2 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
2 ms |
4688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4688 KB |
Output is correct |
2 |
Correct |
2 ms |
4688 KB |
Output is correct |
3 |
Correct |
2 ms |
4688 KB |
Output is correct |
4 |
Correct |
2 ms |
4688 KB |
Output is correct |
5 |
Correct |
2 ms |
4688 KB |
Output is correct |
6 |
Correct |
2 ms |
4688 KB |
Output is correct |
7 |
Correct |
2 ms |
4688 KB |
Output is correct |
8 |
Correct |
2 ms |
4688 KB |
Output is correct |
9 |
Correct |
2 ms |
4656 KB |
Output is correct |
10 |
Correct |
2 ms |
4688 KB |
Output is correct |
11 |
Correct |
2 ms |
4688 KB |
Output is correct |
12 |
Correct |
2 ms |
4688 KB |
Output is correct |
13 |
Correct |
2 ms |
4688 KB |
Output is correct |
14 |
Correct |
2 ms |
4688 KB |
Output is correct |
15 |
Correct |
2 ms |
4688 KB |
Output is correct |
16 |
Correct |
2 ms |
4688 KB |
Output is correct |
17 |
Correct |
2 ms |
4688 KB |
Output is correct |
18 |
Correct |
2 ms |
4688 KB |
Output is correct |
19 |
Correct |
2 ms |
4688 KB |
Output is correct |
20 |
Correct |
2 ms |
4688 KB |
Output is correct |
21 |
Correct |
75 ms |
15060 KB |
Output is correct |
22 |
Correct |
70 ms |
14980 KB |
Output is correct |
23 |
Correct |
70 ms |
15032 KB |
Output is correct |
24 |
Correct |
54 ms |
14776 KB |
Output is correct |
25 |
Correct |
54 ms |
14780 KB |
Output is correct |
26 |
Correct |
51 ms |
14788 KB |
Output is correct |
27 |
Correct |
50 ms |
15040 KB |
Output is correct |
28 |
Correct |
50 ms |
15072 KB |
Output is correct |
29 |
Correct |
50 ms |
15032 KB |
Output is correct |
30 |
Correct |
56 ms |
15168 KB |
Output is correct |
31 |
Correct |
51 ms |
15036 KB |
Output is correct |
32 |
Correct |
50 ms |
15036 KB |
Output is correct |
33 |
Correct |
55 ms |
14780 KB |
Output is correct |
34 |
Correct |
58 ms |
14780 KB |
Output is correct |
35 |
Correct |
54 ms |
14964 KB |
Output is correct |
36 |
Correct |
67 ms |
15036 KB |
Output is correct |
37 |
Correct |
66 ms |
15032 KB |
Output is correct |
38 |
Correct |
67 ms |
15032 KB |
Output is correct |
39 |
Correct |
48 ms |
14776 KB |
Output is correct |
40 |
Correct |
49 ms |
14776 KB |
Output is correct |
41 |
Correct |
48 ms |
14784 KB |
Output is correct |
42 |
Correct |
49 ms |
15032 KB |
Output is correct |
43 |
Correct |
50 ms |
15032 KB |
Output is correct |
44 |
Correct |
50 ms |
14960 KB |
Output is correct |
45 |
Correct |
51 ms |
15032 KB |
Output is correct |
46 |
Correct |
49 ms |
15112 KB |
Output is correct |
47 |
Correct |
55 ms |
15144 KB |
Output is correct |
48 |
Correct |
56 ms |
14776 KB |
Output is correct |
49 |
Correct |
62 ms |
14892 KB |
Output is correct |
50 |
Correct |
57 ms |
14776 KB |
Output is correct |