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 fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(),x.end()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
vii line;
int pos;
int eval(pii a,int x)
{
return a.fi * x + a.se;
}
bool bad(pii a,pii b,pii c)
{
return (double)(b.se - a.se) / (a.fi - b.fi) >= (double)(c.se - a.se) / (a.fi - c.fi);
}
void add(pii newline)
{
line.pb(newline);
while(line.size() > 2 && bad(line[line.size() - 3],line[line.size() - 2],line.back()))
line.erase(line.end() - 2);
}
int get(int x)
{
if(pos >= line.size()) pos = line.size() - 1;
while(pos < line.size() - 1 && eval(line[pos],x) > eval(line[pos + 1],x)) pos++;
return eval(line[pos],x);
}
int f[maxn];
int n,a[maxn];
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
fo(i,1,n)
{
cin >> a[i];
maximize(a[i],a[i - 1]);
f[i] = inf;
}
add({0,0});
fo(i,1,n)
{
f[i] = get(a[i]) + n * a[i];
add({-i,f[i]});
}
cout << f[n];
}
Compilation message (stderr)
Discharging.cpp: In function 'long long int get(long long int)':
Discharging.cpp:61:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if(pos >= line.size()) pos = line.size() - 1;
| ~~~~^~~~~~~~~~~~~~
Discharging.cpp:62:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(pos < line.size() - 1 && eval(line[pos],x) > eval(line[pos + 1],x)) pos++;
| ~~~~^~~~~~~~~~~~~~~~~
Discharging.cpp: At global scope:
Discharging.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
67 | main()
| ^~~~
Discharging.cpp: In function 'int main()':
Discharging.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |