This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#pragma GCC optimize("Ofast")
//1.0 * clock() / CLOCKS_PER_SEC
#define fi first
#define se second
#define ll long long
#define dl double long
using namespace std;
const ll NN = 1e10 + 7;
const int N = 1e5 + 7;
const int M = 6;
const int mod = 1e9 + 7;
const ll inf = 1e18 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
int n;
int p[N];
ll a[N];
set < pair < ll , ll > > s,ss;
int get( int x )
{
return (p[x] == x ? x : p[x] = get(p[x]));
}
void solve1()
{
cin >> n;
for( int i = 1; i <= n; i++ ){
cin >> a[i];
p[i] = i;
ss.insert({ a[i] , i });
}
p[n + 1] = n + 1;
ll ans = 0;
for( int i = 1; i <= (n + 1) / 2; i++ ){
if( ss.empty() )break;
auto x = *(--ss.end());
ss.erase(--ss.end());
ans += x.fi;
cout << ans << " ";
int l = get( x.se - 1 );
int r = get( x.se + 1 );
if( l != 0 ){
ss.erase({ a[l] , l });
}
if( r != n + 1 ){
ss.erase({ a[r] , r });
}
int f = get( x.se );
if( l != 0 && r != n + 1 ){
ll mz = a[l] + a[r] - x.fi;
p[f] = l;
f = get( x.se );
p[r] = f;
a[f] = mz;
ss.insert({ mz , f });
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int cghf = 1;//cin >> cghf;
while( cghf-- ){
solve1();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |