/***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];
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];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |