제출 #516452

#제출 시각아이디문제언어결과실행 시간메모리
516452Leonardo_PaesCandies (JOI18_candies)C++17
100 / 100
697 ms28864 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
#define f first
#define s second
const ll inf = 1e18;

set<pii> s, ord;

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int n;
	cin >> n;
	for(int i=1; i<=n; i++){
		int x;
		cin >> x;
		s.insert({-x, i});
		ord.insert({i, -x});
	}
	s.insert({inf, 0}), s.insert({inf, n+1});
	ord.insert({0, inf}), ord.insert({n+1, inf});
	ll ans = 0;
	for(int i=0; i<((n+1)>>1); i++){
		pii v = *s.begin();
		ans -= v.f;
		cout << ans << endl;
		auto it = ord.lower_bound({v.s, -inf});
		--it;
		pii a = *it;
		s.erase(s.find({a.s, a.f}));
		ord.erase(it++);
		pii b = *it;
		s.erase(s.find({b.s, b.f}));
		ord.erase(it++);
		pii c = *it;
		s.erase(s.find({c.s, c.f}));
		ord.erase(it++);
		s.insert({a.s+c.s-b.s, b.f});
		ord.insert({b.f, a.s+c.s-b.s});
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...