#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;
// struct Cmp1{
// bool operator()(iii a,iii b){
// return a.ff > b.ff;
// }
// }
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];
int beforeLink[n];
int afterLink[n];
set<iii,greater<iii> > links;// greater
FOR(i,n)afterLink[i] = beforeLink[i] = i;
FOR(i,n)links.insert({arr[i],{i,i}});
// set up has been done.
ll cost = 0;
map<ii,ll> mp;
FOR(i,n)mp[{i,i}] = arr[i];
FOR(i,(n+1)/2){
//cout << "PRINTING STUFF : " << endl;
for(auto e : links){
//cout << e.ff << " : " << e.ss.ff << " " << e.ss.ss << endl;
}
iii item = *(links.begin());
cost += item.ff;
links.erase(links.begin());
// get the before item, and delete it;
int st = item.ss.ff;
int en = item.ss.ss;
ll nc = -item.ff;
if(item.ss.ff > 0){
ll bcost = mp[{beforeLink[item.ss.ff-1],item.ss.ff-1}];
links.erase({bcost,{beforeLink[item.ss.ff-1],item.ss.ff-1}});
}
if(item.ss.ss+1 < n){
ll acost = mp[{item.ss.ss+1,afterLink[item.ss.ss+1]}];
links.erase({acost,{item.ss.ss+1,afterLink[item.ss.ss+1]}});
}
if(item.ss.ff > 0 and item.ss.ss +1 <n){
ll bcost = mp[{beforeLink[item.ss.ff-1],item.ss.ff-1}];
ll acost = mp[{item.ss.ss+1,afterLink[item.ss.ss+1]}];
nc += bcost + acost;
afterLink[beforeLink[item.ss.ff-1]] = afterLink[item.ss.ss+1];
beforeLink[afterLink[item.ss.ss+1]] = beforeLink[item.ss.ff-1];
iii newitem = make_pair(nc,make_pair(beforeLink[item.ss.ff-1],afterLink[item.ss.ss+1]));
//cout << "INSERTING: " << newitem.ff << " " << newitem.ss.ff << " " << newitem.ss.ss << endl;
links.insert(newitem);
mp[{beforeLink[item.ss.ff-1],afterLink[item.ss.ss+1]}] = nc;
}
cout << cost << endl;
//cout << endl;
//cout << "==================" << endl;
//cout << endl;
}
return 0;
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:63:12: warning: variable 'e' set but not used [-Wunused-but-set-variable]
for(auto e : links){
^
candies.cpp:70:7: warning: unused variable 'st' [-Wunused-variable]
int st = item.ss.ff;
^~
candies.cpp:71:7: warning: unused variable 'en' [-Wunused-variable]
int en = item.ss.ss;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |