#include <bits/stdc++.h>
using namespace std;
int n;
int h[1000005];
set<int> active;
class state{
public:
int i, j; // join(i,j); (i < j)
long long diff;
state(int x, int y, long long d) : i(x), j(y), diff(d) {}
friend bool operator<(const state& x, const state& y){
return x.diff > y.diff;
}
};
long long computeDiff(int i, int j){
assert(i < j);
return -1ll*(n-j+1)*h[j] -1ll*(n-i+1)*h[i] + 1ll*(n-i+1)*h[j];
}
bool verifyDiff(state cur){
int i = cur.i;
int j = cur.j;
long long diff = cur.diff;
return active.count(i) && active.count(j) && computeDiff(i,j) == diff;
}
int main(){
scanf("%d",&n);
long long ans = 0ll;
int mx = 0;
vector<int> pivots;
for(int i = 1; i <= n; i++){
scanf("%d",h+i);
if(h[i] > mx){
mx = h[i];
pivots.push_back(i);
ans += 1ll * mx * (n-i+1);
}
}
priority_queue<state> pq;
for(int i = 0; i < pivots.size()-1; i++){
int idx = pivots[i];
int nidx = pivots[i+1];
long long diff = computeDiff(idx, nidx);
pq.emplace(idx, nidx, diff);
active.insert(idx);
}
active.insert(pivots.back());
while(!pq.empty() && pq.top().diff < 0ll){
state cur = pq.top();
pq.pop();
if(!verifyDiff(cur)) continue;
ans += cur.diff;
h[cur.i] = h[cur.j];
active.erase(cur.j);
auto it = active.upper_bound(cur.j);
if(it != active.end()){
int nxtj = *it;
long long diff = computeDiff(cur.i, nxtj);
pq.emplace(cur.i, nxtj, diff);
}
it = active.lower_bound(cur.i);
if(it != active.begin()){
--it;
int prej = *it;
long long diff = computeDiff(prej, cur.i);
pq.emplace(prej, cur.i, diff);
}
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Discharging.cpp: In function 'int main()':
Discharging.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < pivots.size()-1; i++){
| ~~^~~~~~~~~~~~~~~~~
Discharging.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
26 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Discharging.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
31 | scanf("%d",h+i);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
4216 KB |
Output is correct |
2 |
Correct |
162 ms |
4216 KB |
Output is correct |
3 |
Correct |
167 ms |
4216 KB |
Output is correct |
4 |
Correct |
163 ms |
4216 KB |
Output is correct |
5 |
Correct |
163 ms |
4216 KB |
Output is correct |
6 |
Correct |
162 ms |
4268 KB |
Output is correct |
7 |
Correct |
165 ms |
4344 KB |
Output is correct |
8 |
Correct |
169 ms |
4220 KB |
Output is correct |
9 |
Correct |
163 ms |
4344 KB |
Output is correct |
10 |
Correct |
167 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |