# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
44889 | PowerOfNinjaGo | Candies (JOI18_candies) | C++17 | 3 ms | 376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Power Of Ninja Go
#include <bits/stdc++.h>
//#ifdef atom #else #endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector< ii > vii;
#define X first
#define Y second
#define pb push_back
const int maxn = 2e5+5;
int pred[maxn], succ[maxn];
int cost[maxn]; bool dong[maxn];
int main()
{
int n; scanf("%d", &n);
for(int i = 1; i<= n; i++)
{
pred[i] = i-1; succ[i] = i+1;
}
for(int i = 1; i<= n; i++) scanf("%d", &cost[i]);
priority_queue< ii > pq;
for(int i = 1; i<= n; i++) pq.push(ii(cost[i], i));
ll ans = 0;
int rounds = (n+1)/2;
while(!pq.empty() && rounds)
{
auto k = pq.top(); pq.pop();
int w = k.X, u = k.Y;
if(dong[u]) continue;
ans += w;
rounds--;
cost[u] = cost[pred[u]]+cost[succ[u]]-cost[u];
dong[pred[u]] = dong[succ[u]] = true;
succ[pred[pred[u]]] = u; pred[u] = pred[pred[u]];
pred[succ[succ[u]]] = u; succ[u] = succ[succ[u]];
pq.push(ii(cost[u], u));
printf("%lld\n", ans);
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |