#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;
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:65:12: warning: variable 'e' set but not used [-Wunused-but-set-variable]
for(auto e: all){
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
768 KB |
Output is correct |
2 |
Correct |
18 ms |
896 KB |
Output is correct |
3 |
Correct |
17 ms |
768 KB |
Output is correct |
4 |
Correct |
17 ms |
896 KB |
Output is correct |
5 |
Correct |
17 ms |
768 KB |
Output is correct |
6 |
Correct |
18 ms |
768 KB |
Output is correct |
7 |
Correct |
17 ms |
768 KB |
Output is correct |
8 |
Correct |
17 ms |
768 KB |
Output is correct |
9 |
Correct |
15 ms |
768 KB |
Output is correct |
10 |
Correct |
15 ms |
768 KB |
Output is correct |
11 |
Correct |
18 ms |
768 KB |
Output is correct |
12 |
Correct |
17 ms |
768 KB |
Output is correct |
13 |
Correct |
18 ms |
896 KB |
Output is correct |
14 |
Correct |
17 ms |
768 KB |
Output is correct |
15 |
Correct |
17 ms |
896 KB |
Output is correct |
16 |
Correct |
17 ms |
896 KB |
Output is correct |
17 |
Correct |
17 ms |
768 KB |
Output is correct |
18 |
Correct |
17 ms |
768 KB |
Output is correct |
19 |
Correct |
15 ms |
768 KB |
Output is correct |
20 |
Correct |
15 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
768 KB |
Output is correct |
2 |
Correct |
18 ms |
896 KB |
Output is correct |
3 |
Correct |
17 ms |
768 KB |
Output is correct |
4 |
Correct |
17 ms |
896 KB |
Output is correct |
5 |
Correct |
17 ms |
768 KB |
Output is correct |
6 |
Correct |
18 ms |
768 KB |
Output is correct |
7 |
Correct |
17 ms |
768 KB |
Output is correct |
8 |
Correct |
17 ms |
768 KB |
Output is correct |
9 |
Correct |
15 ms |
768 KB |
Output is correct |
10 |
Correct |
15 ms |
768 KB |
Output is correct |
11 |
Correct |
18 ms |
768 KB |
Output is correct |
12 |
Correct |
17 ms |
768 KB |
Output is correct |
13 |
Correct |
18 ms |
896 KB |
Output is correct |
14 |
Correct |
17 ms |
768 KB |
Output is correct |
15 |
Correct |
17 ms |
896 KB |
Output is correct |
16 |
Correct |
17 ms |
896 KB |
Output is correct |
17 |
Correct |
17 ms |
768 KB |
Output is correct |
18 |
Correct |
17 ms |
768 KB |
Output is correct |
19 |
Correct |
15 ms |
768 KB |
Output is correct |
20 |
Correct |
15 ms |
768 KB |
Output is correct |
21 |
Execution timed out |
5044 ms |
33636 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |