/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 1e6+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
ll n, a[N], ans;
set<pll> st, rst;
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++) {
cin>>a[i];
st.insert({a[i], i});
rst.insert({i, a[i]});
}
while(size(st) > 0) {
auto tmp = st.end(); tmp--;
pll it = (*tmp);
ans += (it.F);
cout<<ans<<endl;
if(size(st) == 1) break;
if(it.S == (rst.rbegin()->F)) {
st.erase(it);
rst.erase({it.S, it.F});
it = (*rst.rbegin());
st.erase({it.S, it.F});
rst.erase(it);
continue;
} else if (it.S == (rst.begin()->F)) {
st.erase(it);
rst.erase({it.S, it.F});
it = (*rst.begin());
st.erase({it.S, it.F});
rst.erase(it);
continue;
}
auto tmp2 = rst.upper_bound({it.S, it.F});
auto nxt2 = (*tmp2); tmp2--; tmp2--;
auto prev2 = (*tmp2);
ll sztmp = size(rst);
rst.erase(nxt2);
rst.erase(prev2);
rst.erase({it.S, it.F});
st.erase(it);
st.erase({nxt2.S, nxt2.F});
st.erase({prev2.S, prev2.F});
st.insert({nxt2.S+prev2.S-it.F, it.S});
rst.insert({it.S, nxt2.S+prev2.S-it.F});
}
return 0;
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:80:6: warning: unused variable 'sztmp' [-Wunused-variable]
80 | ll sztmp = size(rst);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
768 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
553 ms |
31108 KB |
Output is correct |
22 |
Correct |
537 ms |
31140 KB |
Output is correct |
23 |
Correct |
509 ms |
30976 KB |
Output is correct |
24 |
Correct |
229 ms |
30860 KB |
Output is correct |
25 |
Correct |
233 ms |
30960 KB |
Output is correct |
26 |
Correct |
214 ms |
30796 KB |
Output is correct |
27 |
Correct |
228 ms |
31120 KB |
Output is correct |
28 |
Correct |
242 ms |
31240 KB |
Output is correct |
29 |
Correct |
218 ms |
30908 KB |
Output is correct |
30 |
Correct |
245 ms |
31148 KB |
Output is correct |
31 |
Correct |
246 ms |
30960 KB |
Output is correct |
32 |
Correct |
315 ms |
31224 KB |
Output is correct |
33 |
Correct |
321 ms |
30844 KB |
Output is correct |
34 |
Correct |
338 ms |
31104 KB |
Output is correct |
35 |
Correct |
316 ms |
30756 KB |
Output is correct |
36 |
Correct |
451 ms |
31188 KB |
Output is correct |
37 |
Correct |
456 ms |
30952 KB |
Output is correct |
38 |
Correct |
431 ms |
30940 KB |
Output is correct |
39 |
Correct |
201 ms |
30908 KB |
Output is correct |
40 |
Correct |
202 ms |
30804 KB |
Output is correct |
41 |
Correct |
215 ms |
30804 KB |
Output is correct |
42 |
Correct |
238 ms |
31320 KB |
Output is correct |
43 |
Correct |
221 ms |
31056 KB |
Output is correct |
44 |
Correct |
241 ms |
31172 KB |
Output is correct |
45 |
Correct |
251 ms |
30956 KB |
Output is correct |
46 |
Correct |
242 ms |
31040 KB |
Output is correct |
47 |
Correct |
251 ms |
31052 KB |
Output is correct |
48 |
Correct |
365 ms |
30716 KB |
Output is correct |
49 |
Correct |
359 ms |
31324 KB |
Output is correct |
50 |
Correct |
349 ms |
30752 KB |
Output is correct |