/***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-1;
r[i] = i+1;
}
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) s.erase({a[id[l[i]]] , id[l[i]]});
if(r[i] <= n) s.erase({a[id[r[i]]] , id[r[i]]});
id[l[i]] = id[r[i]] = i;
ll L = l[i] , R = r[i];
if(L == 1) l[i] = 0;
else if(id[L-2] == L-2) l[i] = L-1;
else l[i] = l[id[L-2]];
if(R == n) r[i] = n+1;
else if(id[R+2] == R+2) r[i] = R+1;
else r[i] = r[id[R+2]];
id[l[i]] = id[r[i]] = i;
s.in({a[l[i]] + a[r[i]] - a[i] , i});
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |