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>
using namespace std;
#define ll long long
#define fixbug false
const ll INF = (ll)4e18+7;
const int maxn = 1e5;
ll a[maxn+2] ;
int n;
namespace subtask1{
bool check(){
return n <= 1500;
}
const int N = 1500;
ll f[N+2] , sum[N+2] = {};
void main_code(){
for (int i = 1; i <= n; ++i) f[i] = INF , sum[i] = INF;
for (int i = 1; i <= n; ++i){
ll mx = a[i];
for (int j = i; j >= 1; --j){
mx = max(mx , a[j]);
f[i] = min(f[i] , f[j - 1] + mx * (n - j + 1));
}
}
cout << f[n];
return;
}
}
namespace subtask2{
bool check(){
return true;
}
const int N = 2e5;
ll f[N+2];
#define double long double
struct Line{
ll a , b;
Line(ll a , ll b) : a(a) , b(b) {};
ll F(ll x){
return a * x + b;
}
};
struct Hull{
Line X;
double slope;
};
double get_slope(Line a , Line b){
return (double)(b.b - a.b) / (a.a - b.a);
}
vector<Hull> convex;
void addline(Line x){
int n = convex.size();
while (n > 1 && get_slope(convex[n - 2].X , x) <= convex[n-1].slope){
convex.pop_back();
--n;
}
if (!convex.size()) convex.push_back({x , LLONG_MIN});
else convex.push_back({x , get_slope(convex[n - 1].X,x)});
return;
}
ll Find(ll x , int t){
int l = 0 , r = convex.size() - 1 , p = 0;
while (l<=r){
int m = l + r >> 1;
if (convex[m].slope <= x){
l = m + 1;
p = m;
}
else r = m - 1;
}
if (t&&fixbug){
cout << convex[p].X.a << ' ' << convex[p].X.b << ' ' << p << '\n';
for (auto& x : convex) cout << x.X.a << ' ' << x.X.b << ' ' << x.slope << '\n';
}
return convex[p].X.F(x);
}
void add(ll a , ll b){
addline(Line(a,b));
return;
}
void main_code(){
for (int i = 1; i <= n; ++i){
add(-i , f[i-1]);
f[i] = Find(a[i],i==2) + a[i]*n + a[i];
int j = i;
while (j <= n && a[i] >= a[j]){
++j;
f[j] = f[i];
}
i = j - 1;
if (fixbug){
cout << i << ' ' << f[i] << '\n';
}
}
cout << f[n];
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#define name "main"
if (fopen(name".inp","r")){
freopen(name".inp","r",stdin);
}
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
if (subtask1::check()) {
subtask1::main_code();
exit(0);
}
subtask2::main_code();
exit(0);
}
Compilation message (stderr)
Discharging.cpp: In function 'long long int subtask2::Find(long long int, int)':
Discharging.cpp:67:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int m = l + r >> 1;
| ~~^~~
Discharging.cpp: In function 'int main()':
Discharging.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |