#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <queue>
#include <deque>
#include <iomanip>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define ld long double
//#define int short
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define iii pair<ll,ii>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
//#define mp make_pair
#define vv vector
#define endl '\n'
using namespace std;
const ll INF = 1e18;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int arr[n];
FOR(i,n)cin >> arr[i];
set<iii, greater<iii> > all;
map<ii,ll> values;
int beforeLink[n];
int afterLink[n];
FOR(i,n){
beforeLink[i] = i-1;
afterLink[i]=i+1;
values[{i,i+1}] = arr[i];
all.insert({arr[i],{i,i+1}});
}
ll cost = 0;
FOR(i,(n+1)/2){
iii item = *(all.begin());
cost += item.ff;
all.erase(item);
if(item.ss.ff > 0){
ll acost = values[{beforeLink[item.ss.ff],item.ss.ff}];
all.erase({acost,{beforeLink[item.ss.ff],item.ss.ff}});
}else{
ll bcost = values[{item.ss.ss,afterLink[item.ss.ss]}];
all.erase({bcost,{item.ss.ss,afterLink[item.ss.ss]}});
beforeLink[afterLink[item.ss.ss]] = item.ss.ff;
afterLink[item.ss.ff] = afterLink[item.ss.ss];
iii nc = {-INF, {item.ss.ff,afterLink[item.ss.ss]}};
values[nc.ss] = nc.ff;
all.insert(nc);
cout << cost << endl;
continue;
}
if(item.ss.ss < n){
ll bcost = values[{item.ss.ss,afterLink[item.ss.ss]}];
all.erase({bcost,{item.ss.ss,afterLink[item.ss.ss]}});
}else{
ll acost = values[{beforeLink[item.ss.ff],item.ss.ff}];
all.erase({acost,{beforeLink[item.ss.ff],item.ss.ff}});
beforeLink[item.ss.ss] = beforeLink[item.ss.ff];
afterLink[beforeLink[item.ss.ff]] = item.ss.ss;
iii nc = {-INF, {beforeLink[item.ss.ff],item.ss.ss}};
values[nc.ss] = nc.ff;
all.insert(nc);
cout << cost << endl;
continue;
}
if(item.ss.ff > 0 and item.ss.ss < n){
ll acost = values[{beforeLink[item.ss.ff],item.ss.ff}];
ll bcost = values[{item.ss.ss,afterLink[item.ss.ss]}];
beforeLink[afterLink[item.ss.ss]] = beforeLink[item.ss.ff];
afterLink[beforeLink[item.ss.ff]] = afterLink[item.ss.ss];
iii nc = {acost + bcost - item.ff, {beforeLink[item.ss.ff],afterLink[item.ss.ss]}};
values[nc.ss] = nc.ff;
all.insert(nc);
}
cout << cost << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
640 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
7 ms |
640 KB |
Output is correct |
7 |
Correct |
7 ms |
640 KB |
Output is correct |
8 |
Correct |
7 ms |
768 KB |
Output is correct |
9 |
Correct |
6 ms |
640 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
7 ms |
640 KB |
Output is correct |
12 |
Correct |
7 ms |
640 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
7 ms |
640 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
640 KB |
Output is correct |
18 |
Correct |
7 ms |
640 KB |
Output is correct |
19 |
Correct |
7 ms |
640 KB |
Output is correct |
20 |
Correct |
7 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
640 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
7 ms |
640 KB |
Output is correct |
7 |
Correct |
7 ms |
640 KB |
Output is correct |
8 |
Correct |
7 ms |
768 KB |
Output is correct |
9 |
Correct |
6 ms |
640 KB |
Output is correct |
10 |
Correct |
7 ms |
640 KB |
Output is correct |
11 |
Correct |
7 ms |
640 KB |
Output is correct |
12 |
Correct |
7 ms |
640 KB |
Output is correct |
13 |
Correct |
7 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
7 ms |
640 KB |
Output is correct |
16 |
Correct |
7 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
640 KB |
Output is correct |
18 |
Correct |
7 ms |
640 KB |
Output is correct |
19 |
Correct |
7 ms |
640 KB |
Output is correct |
20 |
Correct |
7 ms |
640 KB |
Output is correct |
21 |
Correct |
720 ms |
29304 KB |
Output is correct |
22 |
Correct |
665 ms |
29432 KB |
Output is correct |
23 |
Correct |
654 ms |
29304 KB |
Output is correct |
24 |
Correct |
340 ms |
29176 KB |
Output is correct |
25 |
Correct |
328 ms |
29180 KB |
Output is correct |
26 |
Correct |
320 ms |
29176 KB |
Output is correct |
27 |
Correct |
385 ms |
29432 KB |
Output is correct |
28 |
Correct |
339 ms |
29264 KB |
Output is correct |
29 |
Correct |
356 ms |
29220 KB |
Output is correct |
30 |
Correct |
355 ms |
29176 KB |
Output is correct |
31 |
Correct |
352 ms |
29176 KB |
Output is correct |
32 |
Correct |
360 ms |
29176 KB |
Output is correct |
33 |
Correct |
467 ms |
29304 KB |
Output is correct |
34 |
Correct |
466 ms |
29396 KB |
Output is correct |
35 |
Correct |
480 ms |
29176 KB |
Output is correct |
36 |
Correct |
648 ms |
29432 KB |
Output is correct |
37 |
Correct |
635 ms |
29208 KB |
Output is correct |
38 |
Correct |
641 ms |
29304 KB |
Output is correct |
39 |
Correct |
351 ms |
29304 KB |
Output is correct |
40 |
Correct |
345 ms |
29176 KB |
Output is correct |
41 |
Correct |
328 ms |
29280 KB |
Output is correct |
42 |
Correct |
337 ms |
29176 KB |
Output is correct |
43 |
Correct |
346 ms |
29304 KB |
Output is correct |
44 |
Correct |
339 ms |
29432 KB |
Output is correct |
45 |
Correct |
355 ms |
29316 KB |
Output is correct |
46 |
Correct |
354 ms |
29304 KB |
Output is correct |
47 |
Correct |
362 ms |
29288 KB |
Output is correct |
48 |
Correct |
473 ms |
29304 KB |
Output is correct |
49 |
Correct |
467 ms |
29344 KB |
Output is correct |
50 |
Correct |
467 ms |
29304 KB |
Output is correct |