#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 0 <= j <= i min(dp[j - 1] + t[i] * (n - j) = (-t[i] * j + dp[j - 1]) + t[i] * n
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, 1, n + 1) scanf("%d", &t[i]);
int mx = 0;
REP (i, 1, n + 1) {
mxto(mx, t[i]);
t[i] = mx;
}
insert(line(0, 0));
REP (i, 1, n + 1) {
dp[i] = query(t[i]) + (ll) t[i] * n;
insert(line(-i, dp[i]));
}
printf("%lld\n", dp[n]);
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:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | REP (i, 1, n + 1) scanf("%d", &t[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
12268 KB |
Output is correct |
2 |
Correct |
182 ms |
12268 KB |
Output is correct |
3 |
Correct |
184 ms |
12268 KB |
Output is correct |
4 |
Correct |
176 ms |
12344 KB |
Output is correct |
5 |
Correct |
175 ms |
12420 KB |
Output is correct |
6 |
Correct |
202 ms |
12268 KB |
Output is correct |
7 |
Correct |
174 ms |
12268 KB |
Output is correct |
8 |
Correct |
175 ms |
12268 KB |
Output is correct |
9 |
Correct |
190 ms |
12268 KB |
Output is correct |
10 |
Correct |
179 ms |
12356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |