#include<bits/stdc++.h>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,fma")
using namespace std;
void solve() {
int n;
cin >> n;
long long a[n];
set<pair<long long,long long>> s, s1;
for(int i = 0;i<n;i++){
cin >> a[i];
s.insert({a[i], i});
s1.insert({i, a[i]});
}
long long sum=0;
for(int i = 1;i<=(n+1)/2;i++) {
long long mx;
int idx;
mx=(*(--s.end())).first;
idx=(*(--s.end())).second;
sum+=mx;
s.erase({mx, idx});
s1.erase({idx, mx});
auto it1=s1.lower_bound({idx+1, 0});
auto it2=it1;
if(s.size()) {
if(it1==s1.end() or it1==s1.begin()) {
if(it1==s1.end())--it1;
long long val=(*it1).second;
s.erase({(*it1).second, (*it1).first});
s1.erase(it1);
}
else {
it2--;
long long sm=(*it1).second+(*it2).second-mx;
s.erase({(*it2).second, (*it2).first});
s1.erase(it2);
s.erase({(*it1).second, (*it1).first});
s1.erase(it1);
s.insert({sm, idx});
s1.insert({idx, sm});
}
}
cout << sum << "\n";
}
}
int main () {
cin.tie(0)->sync_with_stdio(0);
int t=1;
while(t--)solve();
return 0;
}
Compilation message
candies.cpp: In function 'void solve()':
candies.cpp:29:19: warning: unused variable 'val' [-Wunused-variable]
29 | long long val=(*it1).second;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
580 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
580 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
428 ms |
30416 KB |
Output is correct |
22 |
Correct |
410 ms |
30316 KB |
Output is correct |
23 |
Correct |
421 ms |
30280 KB |
Output is correct |
24 |
Correct |
187 ms |
30152 KB |
Output is correct |
25 |
Correct |
187 ms |
30152 KB |
Output is correct |
26 |
Correct |
188 ms |
30180 KB |
Output is correct |
27 |
Correct |
200 ms |
30296 KB |
Output is correct |
28 |
Correct |
196 ms |
30248 KB |
Output is correct |
29 |
Correct |
193 ms |
30252 KB |
Output is correct |
30 |
Correct |
211 ms |
30348 KB |
Output is correct |
31 |
Correct |
206 ms |
30360 KB |
Output is correct |
32 |
Correct |
203 ms |
30244 KB |
Output is correct |
33 |
Correct |
301 ms |
30120 KB |
Output is correct |
34 |
Correct |
282 ms |
30132 KB |
Output is correct |
35 |
Correct |
303 ms |
30180 KB |
Output is correct |
36 |
Correct |
403 ms |
30284 KB |
Output is correct |
37 |
Correct |
415 ms |
30292 KB |
Output is correct |
38 |
Correct |
397 ms |
30280 KB |
Output is correct |
39 |
Correct |
209 ms |
30104 KB |
Output is correct |
40 |
Correct |
184 ms |
30156 KB |
Output is correct |
41 |
Correct |
185 ms |
30196 KB |
Output is correct |
42 |
Correct |
207 ms |
30284 KB |
Output is correct |
43 |
Correct |
195 ms |
30356 KB |
Output is correct |
44 |
Correct |
193 ms |
30296 KB |
Output is correct |
45 |
Correct |
203 ms |
30284 KB |
Output is correct |
46 |
Correct |
213 ms |
30284 KB |
Output is correct |
47 |
Correct |
215 ms |
30352 KB |
Output is correct |
48 |
Correct |
273 ms |
30136 KB |
Output is correct |
49 |
Correct |
284 ms |
30132 KB |
Output is correct |
50 |
Correct |
292 ms |
30136 KB |
Output is correct |