#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() > 2 && intersect(dq.back(), cur) <= intersect(dq[dq.size() - 2], cur)) {
dq.pop_back();
}
dq.push_back(cur);
}
ll query(ll x) {
while (dq.size() > 2 && 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
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 |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
21916 KB |
Output is correct |
2 |
Correct |
176 ms |
21740 KB |
Output is correct |
3 |
Correct |
178 ms |
21908 KB |
Output is correct |
4 |
Correct |
173 ms |
21740 KB |
Output is correct |
5 |
Correct |
176 ms |
21740 KB |
Output is correct |
6 |
Correct |
176 ms |
21740 KB |
Output is correct |
7 |
Correct |
178 ms |
21740 KB |
Output is correct |
8 |
Correct |
178 ms |
21740 KB |
Output is correct |
9 |
Correct |
172 ms |
21740 KB |
Output is correct |
10 |
Correct |
173 ms |
21740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |