This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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].s)) {
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 || l > id || r < id) return inf * inf;
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] = {0,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 (stderr)
Discharging.cpp:36:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
36 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |