//how tf editorial...
#include <bits/stdc++.h>
using namespace std;
//loops (warning : long long)
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r - 1); i >= l; --i)
//pairs, tuples
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
//vectors
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sum_of(v) accumulate(all(v), 0ll)
#define sz(v) (int)v.size()
#define compact(v) v.erase(unique(all(v)), end(v))
//binary search
#define lwb lower_bound
#define upb upper_bound
//other stuffs
#define dbg(x) "[" #x " = " << (x) << "]"
#define file(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using db = double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAX = 2e5 + 5;
int N, A[MAX];
ll val[MAX], ans[MAX];
multiset<int> online;
void testcase(int ntestcase){
cin >> N;
FOR(i, 0, N) cin >> A[i], val[i] = A[i];
priority_queue<pair<ll, int>> cur, deleted;
FOR(i, 0, N) {
cur.push(mp(A[i], i));
online.insert(i);
}
ll sum = 0;
FOR(i, 1, (N+1)/2 + 1){
while(!deleted.empty() && deleted.top() == cur.top()){
cur.pop(); deleted.pop();
}
pair<ll, int> best = cur.top(); cur.pop();
sum += best.first;
ans[i] = sum;
multiset<int>::iterator b = online.lower_bound(best.second);
ll earn = 0;
if(b != online.begin()){
int L = *prev(b);
earn += val[L];
deleted.push(mp(val[L], L));
online.erase(L);
}
if(next(b) != online.end()){
int R = *next(b);
earn += val[R];
deleted.push(mp(val[R], R));
online.erase(R);
}
val[best.second] = earn - best.first;
cur.push(mp(val[best.second], best.second));
}
FOR(i, 1, (N+1)/2 + 1){
cout << ans[i] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
int T = 1;
// cin >> T;
FOR(i, 0, T) testcase(i);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |