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 <iostream>
#include <deque>
using namespace std;
struct Line
{
long long m, n;
long long eval(long long x)
{
return m * x + n;
}
long double intersect(Line l)
{
return (1.0 * (l.n - n)) / (1.0 * (m - l.m));
}
};
struct CHT
{
deque <Line> dq;
void Insert(Line l)
{
while((int)dq.size() > 1 && dq.end()[-1].intersect(l) < dq.end()[-2].intersect(dq.end()[-1]))
dq.pop_back();
dq.push_back(l);
}
long long Query(long long x)
{
while(dq.size() > 1 && dq[0].intersect(dq[1]) < x)
dq.pop_front();
return dq[0].eval(x);
}
};
const int NMAX = 1e6;
int N;
long long T[NMAX + 5], dp[NMAX + 5];
CHT cht;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for(int i = 1; i <= N; i++)
cin >> T[i];
dp[0] = 0;
cht.Insert({-1, 0});
long long runningMax = 0;
for(int i = 1; i <= N; i++)
{
runningMax = max(runningMax, T[i]);
dp[i] = runningMax * (N + 1) + cht.Query(runningMax);
cht.Insert({-(i + 1), dp[i]});
}
cout << dp[N] << '\n';
return 0;
}
# | 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... |