#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 1e6 + 5, inf = 1e9 + 7; // !
int t,r[N],a[N],n, le_[30 * N], ri_[30*N], cur;
pii tree[30 * N];
int f(int k,int x,int b) {
return k * x + b;
}
void insert(int u,int l,int r,int k,int b) {
if(l == r) {
if(f(tree[u].f,l,tree[u].s) > f(k,l,b)) tree[u] = {k,b};
return;
}
int mid = (l + r)/2;
if(k > tree[u].f) swap(tree[u].f,k), swap(tree[u].s,b);
if(f(k,mid,b) <= f(tree[u].f,mid,tree[u].f)) {
if(!ri_[u]) ri_[u] = ++cur, tree[cur] = tree[u];
else insert(ri_[u],mid + 1,r, tree[u].f, tree[u].s);
tree[u] = {k,b};
}
else {
if(!le_[u]) le_[u] = ++cur, tree[cur] = {k,b};
else insert(le_[u],l,mid,k,b);
}
}
int get(int u,int l,int r,int id) {
if(!u) return inf * id;
if(l == r) return f(tree[u].f, id, tree[u].s);
int mid = (l + r)/2;
return min(f(tree[u].f,id,tree[u].s), min(get(le_[u],l,mid,id), get(ri_[u],mid+1,r,id)));
}
main(){
cin >> n;
int Mx = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
Mx = max(Mx,a[i]);
}
for(int i = n; i >= 1; i--) {
int x = i + 1;
while(x < n + 1 && a[x] <= a[i]) x = r[x];
r[i] = x;
}
int mx = 0;
cur = 1;
tree[1] = {inf,inf};
for(int i = 1; i <= n; i++) {
if(a[i] > mx) {
int x = min( n * a[i], get(1,1,Mx,a[i]));
insert(1,1,Mx,n - r[i] + 1, x);
if(a[i] == Mx) {
cout << x << endl;
return 0;
}
}
mx = max(mx,a[i]);
}
}
Compilation message
Discharging.cpp:36:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
36 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
216 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
464 ms |
25548 KB |
Output is correct |
2 |
Correct |
447 ms |
25584 KB |
Output is correct |
3 |
Correct |
449 ms |
25712 KB |
Output is correct |
4 |
Correct |
453 ms |
25588 KB |
Output is correct |
5 |
Correct |
444 ms |
25636 KB |
Output is correct |
6 |
Correct |
452 ms |
25572 KB |
Output is correct |
7 |
Correct |
459 ms |
25544 KB |
Output is correct |
8 |
Correct |
469 ms |
25632 KB |
Output is correct |
9 |
Correct |
449 ms |
25624 KB |
Output is correct |
10 |
Correct |
448 ms |
25600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
216 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
15 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
216 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
300 KB |
Output is correct |
15 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |