#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;
const int MAXN = 3*100*1000;
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;
unordered_map<int,ll> values;
values.reserve(1024);
values.max_load_factor(0.25);
int beforeLink[n];
int afterLink[n];
FOR(i,n){
beforeLink[i] = i-1;
afterLink[i]=i+1;
values[i*MAXN+i+1] = arr[i];
all.insert({arr[i],{i,i+1}});
}
ll cost = 0;
FOR(i,(n+1)/2){
iii item = *(all.begin());
//cout << " =========== " << endl;
/*for(auto e: all){
//cout << e.ff << " : " << e.ss.ff << " " << e.ss.ss << endl;
}*/
cost += item.ff;
all.erase(item);
if(item.ss.ff > 0){
ll acost = values[beforeLink[item.ss.ff]*MAXN+item.ss.ff];
all.erase({acost,{beforeLink[item.ss.ff],item.ss.ff}});
}else{
ll bcost = values[item.ss.ss*MAXN+ 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.ff*MAXN+nc.ss.ss] = nc.ff;
all.insert(nc);
cout << cost << endl;
continue;
}
if(item.ss.ss < n){
ll bcost = values[item.ss.ss*MAXN+afterLink[item.ss.ss]];
all.erase({bcost,{item.ss.ss,afterLink[item.ss.ss]}});
}else{
ll acost = values[beforeLink[item.ss.ff]*MAXN+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.ff*MAXN+nc.ss.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]*MAXN+item.ss.ff];
ll bcost = values[item.ss.ss*MAXN+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.ff*MAXN+nc.ss.ss] = nc.ff;
all.insert(nc);
}
cout << cost << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
768 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
8 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
896 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
7 ms |
768 KB |
Output is correct |
8 |
Correct |
6 ms |
768 KB |
Output is correct |
9 |
Correct |
6 ms |
768 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
12 |
Correct |
6 ms |
768 KB |
Output is correct |
13 |
Correct |
6 ms |
896 KB |
Output is correct |
14 |
Correct |
7 ms |
768 KB |
Output is correct |
15 |
Correct |
7 ms |
768 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
6 ms |
768 KB |
Output is correct |
18 |
Correct |
6 ms |
768 KB |
Output is correct |
19 |
Correct |
6 ms |
768 KB |
Output is correct |
20 |
Correct |
6 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
768 KB |
Output is correct |
2 |
Correct |
6 ms |
768 KB |
Output is correct |
3 |
Correct |
8 ms |
768 KB |
Output is correct |
4 |
Correct |
6 ms |
768 KB |
Output is correct |
5 |
Correct |
6 ms |
896 KB |
Output is correct |
6 |
Correct |
6 ms |
768 KB |
Output is correct |
7 |
Correct |
7 ms |
768 KB |
Output is correct |
8 |
Correct |
6 ms |
768 KB |
Output is correct |
9 |
Correct |
6 ms |
768 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
6 ms |
768 KB |
Output is correct |
12 |
Correct |
6 ms |
768 KB |
Output is correct |
13 |
Correct |
6 ms |
896 KB |
Output is correct |
14 |
Correct |
7 ms |
768 KB |
Output is correct |
15 |
Correct |
7 ms |
768 KB |
Output is correct |
16 |
Correct |
6 ms |
896 KB |
Output is correct |
17 |
Correct |
6 ms |
768 KB |
Output is correct |
18 |
Correct |
6 ms |
768 KB |
Output is correct |
19 |
Correct |
6 ms |
768 KB |
Output is correct |
20 |
Correct |
6 ms |
768 KB |
Output is correct |
21 |
Correct |
518 ms |
33904 KB |
Output is correct |
22 |
Correct |
508 ms |
35532 KB |
Output is correct |
23 |
Correct |
515 ms |
35760 KB |
Output is correct |
24 |
Correct |
258 ms |
35408 KB |
Output is correct |
25 |
Correct |
257 ms |
35404 KB |
Output is correct |
26 |
Correct |
258 ms |
35316 KB |
Output is correct |
27 |
Correct |
281 ms |
35664 KB |
Output is correct |
28 |
Correct |
278 ms |
35576 KB |
Output is correct |
29 |
Correct |
284 ms |
35660 KB |
Output is correct |
30 |
Correct |
293 ms |
35532 KB |
Output is correct |
31 |
Correct |
306 ms |
35532 KB |
Output is correct |
32 |
Correct |
294 ms |
35532 KB |
Output is correct |
33 |
Correct |
392 ms |
35276 KB |
Output is correct |
34 |
Correct |
387 ms |
35276 KB |
Output is correct |
35 |
Correct |
393 ms |
35500 KB |
Output is correct |
36 |
Correct |
501 ms |
35532 KB |
Output is correct |
37 |
Correct |
515 ms |
35532 KB |
Output is correct |
38 |
Correct |
529 ms |
35544 KB |
Output is correct |
39 |
Correct |
258 ms |
35312 KB |
Output is correct |
40 |
Correct |
264 ms |
35408 KB |
Output is correct |
41 |
Correct |
262 ms |
35316 KB |
Output is correct |
42 |
Correct |
280 ms |
35532 KB |
Output is correct |
43 |
Correct |
282 ms |
35532 KB |
Output is correct |
44 |
Correct |
285 ms |
35532 KB |
Output is correct |
45 |
Correct |
300 ms |
35740 KB |
Output is correct |
46 |
Correct |
296 ms |
35532 KB |
Output is correct |
47 |
Correct |
315 ms |
35568 KB |
Output is correct |
48 |
Correct |
381 ms |
35276 KB |
Output is correct |
49 |
Correct |
394 ms |
35444 KB |
Output is correct |
50 |
Correct |
396 ms |
35192 KB |
Output is correct |