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 mnto(x, y) x = min(x, (__typeof__(x)) y)
#define mxto(x, y) x = max(x, (__typeof__(x)) y)
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 1000005
// make t[i] non-decreasing
// define dp[i] as the minimum sum of waiting time up to i with the group ending at i
// dp[i] = for all -1 <= j < i min(dp[j] + t[i] * (n - j - 1)) = (-t[i] * j + dp[j]) + t[i] * (n - 1)
int n;
int t[MAXN];
stack<int> stk;
ll dp[MAXN];
struct line {
ll m, c;
line() {}
line(ll m, ll c): m(m), c(c) {}
ll eval(ll x) {
return m * x + c;
}
};
ld intersect(line a, line b) {
return (ld) (a.c - b.c) / (b.m - a.m);
}
deque<line> dq;
void insert(line cur) {
while (dq.size() > 1 && intersect(dq.back(), cur) <= intersect(dq[dq.size() - 2], cur)) {
dq.pop_back();
}
dq.push_back(cur);
}
ll query(ll x) {
while (dq.size() > 1 && intersect(dq.front(), dq[1]) < x) {
dq.pop_front();
}
return dq.front().eval(x);
}
int main() {
scanf("%d", &n);
REP (i, 0, n) scanf("%d", &t[i]);
int mx = 0;
REP (i, 0, n) {
mxto(mx, t[i]);
t[i] = mx;
}
insert(line(1, 0));
REP (i, 0, n) {
dp[i] = query(t[i]) + (ll) t[i] * (n - 1);
insert(line(-i, dp[i]));
}
printf("%lld\n", dp[n - 1]);
return 0;
}
Compilation message (stderr)
Discharging.cpp: In function 'int main()':
Discharging.cpp:64:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
64 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Discharging.cpp:65:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | REP (i, 0, n) scanf("%d", &t[i]);
| ~~~~~^~~~~~~~~~~~~
# | 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... |