/***Farhan132***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<ll> vll;
typedef pair<ll , ll> ii;
typedef vector< ii > vii;
typedef pair < ll , pair < ll , ll > > cm;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define PI 3.14159265359
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcountll(n)
#define clean fflush(stdout)
const ll mod = (ll)1e9 + 7;
const ll maxn = (ll)2e5 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = 1e9;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
mt19937 rnd(89124);
ll l[maxn] , r[maxn] , id[maxn] , a[maxn];
ll mid(ll l, ll r){
return (l+r)/2;
}
void solve(){
ll n;
cin >> n;
set < ii > s;
f0(maxn) id[i] = i;
a[0] = a[n+1] = 0;
for(ll i = 1; i <= n; i++){
cin >> a[i];
s.in({a[i] , i});
id[i] = i;
l[i] = i;
r[i] = i;
}
r[n+1] = n+1;
l[0] = 0;
ll ans = 0;
ll m = (n+1)/2;
while(m--){
auto [val , i] = *s.rbegin();
s.erase({val , i});
ans += val;
cout << ans << '\n';
if(l[i]-1 >= 1) s.erase({a[id[l[i]-1]] , id[l[i]-1]});
if(r[i]+1 <= n) s.erase({a[id[r[i]+1]] , id[r[i]+1]});
//id[l[i]] = id[r[i]] = i;
//if(l[i] == 0 || r[i] == n+1) continue;
ll L = l[i] , R = r[i];
if(id[L-1] == L-1) l[i] = L-1;
else l[i] = l[id[L-1]] , a[i] -= a[id[L-1]] - a[l[i]];
if(id[R+1] == R+1) r[i] = R+1;
else r[i] = r[id[R+1]], a[i] -= a[id[R+1]] - a[r[i]];
ll s1 = id[L] ,s2 = id[R];
id[l[i]] = id[r[i]] = id[l[i]+1] = id[r[i]-1] = i;
// cout << i << ' ' << L << ' ' << R << " " << ' ' << l[i] << ' ' << r[i] << ' ' << s1 << ' ' << s2 << '\n';
a[i] = a[l[i]] + a[r[i]] - a[i];
if(l[i] != 0 && r[i] != n+1)s.in({a[i], i}) ;
//else bug;
}
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
// freopen("cbarn2.in", "r", stdin);
// freopen("cbarn2.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//cout << fixed << setprecision(10);
// work();
// mem(b,0);
ll T;
T=1;
//cin >> T;
//scanf("%lld",&T);
// ll CT = 0;
//work();
while(T--){
//cout << "Case " << ++CT <<": " ;
//printf("Case %lld: ",++CT);
solve();
}
return 0;
}
Compilation message
candies.cpp: In function 'void solve()':
candies.cpp:91:8: warning: unused variable 's1' [-Wunused-variable]
91 | ll s1 = id[L] ,s2 = id[R];
| ^~
candies.cpp:91:20: warning: unused variable 's2' [-Wunused-variable]
91 | ll s1 = id[L] ,s2 = id[R];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2156 KB |
Output is correct |
2 |
Correct |
3 ms |
2156 KB |
Output is correct |
3 |
Correct |
4 ms |
2156 KB |
Output is correct |
4 |
Correct |
3 ms |
2156 KB |
Output is correct |
5 |
Correct |
3 ms |
2156 KB |
Output is correct |
6 |
Correct |
3 ms |
2156 KB |
Output is correct |
7 |
Correct |
3 ms |
2156 KB |
Output is correct |
8 |
Correct |
3 ms |
2156 KB |
Output is correct |
9 |
Correct |
2 ms |
2156 KB |
Output is correct |
10 |
Correct |
3 ms |
2156 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
3 ms |
2156 KB |
Output is correct |
13 |
Correct |
3 ms |
2156 KB |
Output is correct |
14 |
Correct |
3 ms |
2156 KB |
Output is correct |
15 |
Correct |
3 ms |
2284 KB |
Output is correct |
16 |
Correct |
3 ms |
2156 KB |
Output is correct |
17 |
Correct |
3 ms |
2156 KB |
Output is correct |
18 |
Correct |
3 ms |
2156 KB |
Output is correct |
19 |
Correct |
3 ms |
2156 KB |
Output is correct |
20 |
Correct |
2 ms |
2156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2156 KB |
Output is correct |
2 |
Correct |
3 ms |
2156 KB |
Output is correct |
3 |
Correct |
4 ms |
2156 KB |
Output is correct |
4 |
Correct |
3 ms |
2156 KB |
Output is correct |
5 |
Correct |
3 ms |
2156 KB |
Output is correct |
6 |
Correct |
3 ms |
2156 KB |
Output is correct |
7 |
Correct |
3 ms |
2156 KB |
Output is correct |
8 |
Correct |
3 ms |
2156 KB |
Output is correct |
9 |
Correct |
2 ms |
2156 KB |
Output is correct |
10 |
Correct |
3 ms |
2156 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
3 ms |
2156 KB |
Output is correct |
13 |
Correct |
3 ms |
2156 KB |
Output is correct |
14 |
Correct |
3 ms |
2156 KB |
Output is correct |
15 |
Correct |
3 ms |
2284 KB |
Output is correct |
16 |
Correct |
3 ms |
2156 KB |
Output is correct |
17 |
Correct |
3 ms |
2156 KB |
Output is correct |
18 |
Correct |
3 ms |
2156 KB |
Output is correct |
19 |
Correct |
3 ms |
2156 KB |
Output is correct |
20 |
Correct |
2 ms |
2156 KB |
Output is correct |
21 |
Correct |
413 ms |
22636 KB |
Output is correct |
22 |
Correct |
417 ms |
22556 KB |
Output is correct |
23 |
Correct |
412 ms |
22636 KB |
Output is correct |
24 |
Correct |
173 ms |
22508 KB |
Output is correct |
25 |
Correct |
168 ms |
22380 KB |
Output is correct |
26 |
Correct |
170 ms |
22636 KB |
Output is correct |
27 |
Correct |
182 ms |
22764 KB |
Output is correct |
28 |
Correct |
182 ms |
22608 KB |
Output is correct |
29 |
Correct |
177 ms |
22636 KB |
Output is correct |
30 |
Correct |
194 ms |
22704 KB |
Output is correct |
31 |
Correct |
196 ms |
22636 KB |
Output is correct |
32 |
Correct |
193 ms |
22636 KB |
Output is correct |
33 |
Correct |
292 ms |
22380 KB |
Output is correct |
34 |
Correct |
300 ms |
22636 KB |
Output is correct |
35 |
Correct |
290 ms |
22380 KB |
Output is correct |
36 |
Correct |
416 ms |
22636 KB |
Output is correct |
37 |
Correct |
409 ms |
22508 KB |
Output is correct |
38 |
Correct |
439 ms |
22704 KB |
Output is correct |
39 |
Correct |
191 ms |
22508 KB |
Output is correct |
40 |
Correct |
167 ms |
22508 KB |
Output is correct |
41 |
Correct |
168 ms |
22380 KB |
Output is correct |
42 |
Correct |
178 ms |
22636 KB |
Output is correct |
43 |
Correct |
179 ms |
22636 KB |
Output is correct |
44 |
Correct |
181 ms |
22636 KB |
Output is correct |
45 |
Correct |
193 ms |
22636 KB |
Output is correct |
46 |
Correct |
193 ms |
22636 KB |
Output is correct |
47 |
Correct |
192 ms |
22636 KB |
Output is correct |
48 |
Correct |
302 ms |
22508 KB |
Output is correct |
49 |
Correct |
295 ms |
22380 KB |
Output is correct |
50 |
Correct |
296 ms |
22380 KB |
Output is correct |