#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 2e5+5;
const string NAME = "";
struct seg{
ll val;
int l,r;
seg(ll a, int b, int c){
val=a, l=b, r=c;
}
bool operator<(const seg& a) const{
return val>a.val;
}
};
int n,a[MAXN],l[MAXN],r[MAXN];
ll rs=0,sum[MAXN],pre[MAXN];
bool del[MAXN];
multiset<seg> ms;
bool check(const seg& cur){
if(cur.l==cur.r) return del[cur.l-1]||del[cur.l]||del[cur.l+1];
return 0;
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<=n; ++i)
cin >> a[i], ms.ins(seg(a[i],i,i)), sum[i]=a[i], pre[i]=pre[i-1]+a[i];
iota(l+1,l+n+1,1);
iota(r+1,r+n+1,1);
for(int i = 1; i<=(n+1)/2; ++i){
while(check(*ms.begin())) ms.erase(ms.begin());
seg cur=*ms.begin();
ms.erase(ms.begin()), rs+=cur.val;
cout << rs << "\n";
int L=cur.l, R=cur.r;
ll S=cur.val+(cur.l==cur.r ? 0 : sum[R-1]);
del[L]=del[R]=1;
if(L>2&&del[L-2]){
if(l[L-2]!=1) ms.erase(seg(pre[L-1]-pre[l[L-2]-2]-2*sum[L-2],l[L-2]-1,L-1));
S+=sum[L-2], L=l[L-2];
}
if(R<n-1&&del[R+2]){
if(r[R+2]!=n) ms.erase(seg(pre[r[R+2]+1]-pre[R]-2*sum[r[R+2]],R+1,r[R+2]+1));
R=r[R+2], S+=sum[R];
}
r[R]=L, l[L]=R, sum[R]=S;
if(L!=1&&R!=n) ms.ins(seg(pre[R+1]-pre[L-2]-2*S,L-1,R+1));
}
}
Compilation message (stderr)
candies.cpp: In function 'int main()':
candies.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:43:45: note: in expansion of macro 'fo'
43 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
candies.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:43:45: note: in expansion of macro 'fo'
43 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |