# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
48476 | yp155136 | Candies (JOI18_candies) | C++14 | 139 ms | 20532 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.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
#define F first
#define S second
const int N = 200006;
const LL INF = (1LL<<40);
bool vis[N];
LL cost[N];
int lc[N];
int rc[N];
int main ()
{
int n;
scanf("%d",&n);
priority_queue<pii> pq;
for (int i=1;n>=i;i++)
{
scanf("%lld",&cost[i]);
lc[i] = i-1;
rc[i] = i+1;
pq.push(make_pair(cost[i],i));
}
cost[0] = cost[n+1] = -INF;
LL tot=0;
int cnt=(n+1)/2;
while (!pq.empty())
{
pii p=pq.top();
pq.pop();
if (vis[p.S]) continue;
tot += p.F;
LL newcost = cost[ lc[p.S] ] + cost[ rc[p.S] ] - cost[p.S];
vis[ lc[p.S] ] = vis[ rc[p.S] ] = true;
lc[p.S] = lc[ lc[p.S] ];
rc[p.S] = rc[ rc[p.S] ];
rc[ lc[p.S] ] = p.S;
lc[ rc[p.S] ] = p.S;
cost[p.S] = newcost;
printf("%lld\n",tot);
--cnt;
pq.push(make_pair(cost[p.S],p.S));
if (!cnt) break;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |