#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7;
signed main()
{
IO_OP;
int n;
cin >> n;
V<ll> a(n);
set<int> alive;
priority_queue<pair<ll, int>> pq;
for(int i = 0; i < n; i++) {
cin >> a[i];
alive.insert(i);
pq.push({a[i], i});
}
ll ans = 0;
for(int i = 0; i < (n + 1) / 2; i++) {
auto p = pq.top(); pq.pop();
if(alive.count(p.S) == 0) {
i--;
continue;
}
auto it = alive.find(p.S);
ans += p.F;
if(it != alive.begin() && next(it) != alive.end()) {
int l = *prev(it), r = *next(it);
alive.erase(l), alive.erase(r);
a[p.S] = a[l] + a[r] - a[p.S];
pq.push({a[p.S], p.S});
} else {
if(it != alive.begin()) alive.erase(prev(it));
if(next(it) != alive.end()) alive.erase(next(it));
}
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |