#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 3e5 + 13;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, K;
ll arr[MAXN], pref[MAXN];
ll dp[MAXN], num[MAXN];
void check(ll P) //penalty P
{
int best = 0; ll bestval = 0;
dp[0] = 0;
num[0] = 0;
FOR(i, 0, N)
{
dp[i + 1] = dp[i];
num[i + 1] = num[i];
//or you do like k...i
ll cur = bestval + pref[i + 1] - P;
if (cur > dp[i + 1])
{
dp[i + 1] = cur;
num[i + 1] = num[best] + 1;
}
if (dp[i + 1] - pref[i + 1] > bestval)
{
bestval = dp[i + 1] - pref[i + 1];
best = i + 1;
}
}
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
FOR(i, 0, N)
{
cin >> arr[i];
pref[i + 1] = pref[i] + arr[i];
}
ll lo = 0, hi = 4e14;
while(hi > lo)
{
ll mid = (hi + lo) >> 1;
check(mid);
if (num[N] > K) lo = mid + 1;
else hi = mid;
}
check(lo);
cout << dp[N] + lo * num[N] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
12268 KB |
Output is correct |
2 |
Correct |
76 ms |
12524 KB |
Output is correct |
3 |
Correct |
76 ms |
12652 KB |
Output is correct |
4 |
Correct |
77 ms |
12524 KB |
Output is correct |
5 |
Correct |
78 ms |
12664 KB |
Output is correct |
6 |
Correct |
75 ms |
12268 KB |
Output is correct |
7 |
Correct |
75 ms |
12268 KB |
Output is correct |
8 |
Correct |
76 ms |
12524 KB |
Output is correct |
9 |
Correct |
75 ms |
12268 KB |
Output is correct |
10 |
Correct |
76 ms |
12396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
10604 KB |
Output is correct |
2 |
Correct |
67 ms |
10916 KB |
Output is correct |
3 |
Correct |
69 ms |
10604 KB |
Output is correct |
4 |
Correct |
67 ms |
10732 KB |
Output is correct |
5 |
Correct |
75 ms |
12268 KB |
Output is correct |
6 |
Correct |
66 ms |
10604 KB |
Output is correct |
7 |
Correct |
66 ms |
10860 KB |
Output is correct |
8 |
Correct |
76 ms |
12524 KB |
Output is correct |
9 |
Correct |
74 ms |
12268 KB |
Output is correct |
10 |
Correct |
67 ms |
10860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
12672 KB |
Output is correct |
2 |
Correct |
94 ms |
12524 KB |
Output is correct |
3 |
Correct |
93 ms |
12652 KB |
Output is correct |
4 |
Correct |
92 ms |
12524 KB |
Output is correct |
5 |
Correct |
92 ms |
12652 KB |
Output is correct |
6 |
Correct |
95 ms |
12652 KB |
Output is correct |
7 |
Correct |
93 ms |
12908 KB |
Output is correct |
8 |
Correct |
107 ms |
12652 KB |
Output is correct |
9 |
Correct |
96 ms |
12908 KB |
Output is correct |
10 |
Correct |
97 ms |
12780 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 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 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 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 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 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
12268 KB |
Output is correct |
2 |
Correct |
76 ms |
12524 KB |
Output is correct |
3 |
Correct |
76 ms |
12652 KB |
Output is correct |
4 |
Correct |
77 ms |
12524 KB |
Output is correct |
5 |
Correct |
78 ms |
12664 KB |
Output is correct |
6 |
Correct |
75 ms |
12268 KB |
Output is correct |
7 |
Correct |
75 ms |
12268 KB |
Output is correct |
8 |
Correct |
76 ms |
12524 KB |
Output is correct |
9 |
Correct |
75 ms |
12268 KB |
Output is correct |
10 |
Correct |
76 ms |
12396 KB |
Output is correct |
11 |
Correct |
66 ms |
10604 KB |
Output is correct |
12 |
Correct |
67 ms |
10916 KB |
Output is correct |
13 |
Correct |
69 ms |
10604 KB |
Output is correct |
14 |
Correct |
67 ms |
10732 KB |
Output is correct |
15 |
Correct |
75 ms |
12268 KB |
Output is correct |
16 |
Correct |
66 ms |
10604 KB |
Output is correct |
17 |
Correct |
66 ms |
10860 KB |
Output is correct |
18 |
Correct |
76 ms |
12524 KB |
Output is correct |
19 |
Correct |
74 ms |
12268 KB |
Output is correct |
20 |
Correct |
67 ms |
10860 KB |
Output is correct |
21 |
Correct |
95 ms |
12672 KB |
Output is correct |
22 |
Correct |
94 ms |
12524 KB |
Output is correct |
23 |
Correct |
93 ms |
12652 KB |
Output is correct |
24 |
Correct |
92 ms |
12524 KB |
Output is correct |
25 |
Correct |
92 ms |
12652 KB |
Output is correct |
26 |
Correct |
95 ms |
12652 KB |
Output is correct |
27 |
Correct |
93 ms |
12908 KB |
Output is correct |
28 |
Correct |
107 ms |
12652 KB |
Output is correct |
29 |
Correct |
96 ms |
12908 KB |
Output is correct |
30 |
Correct |
97 ms |
12780 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
512 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
492 KB |
Output is correct |
52 |
Correct |
1 ms |
460 KB |
Output is correct |
53 |
Correct |
1 ms |
492 KB |
Output is correct |
54 |
Correct |
1 ms |
492 KB |
Output is correct |
55 |
Correct |
1 ms |
492 KB |
Output is correct |
56 |
Correct |
1 ms |
492 KB |
Output is correct |
57 |
Correct |
1 ms |
492 KB |
Output is correct |
58 |
Correct |
1 ms |
492 KB |
Output is correct |
59 |
Correct |
1 ms |
492 KB |
Output is correct |
60 |
Correct |
1 ms |
492 KB |
Output is correct |
61 |
Correct |
134 ms |
12652 KB |
Output is correct |
62 |
Correct |
128 ms |
12908 KB |
Output is correct |
63 |
Correct |
104 ms |
12396 KB |
Output is correct |
64 |
Correct |
139 ms |
12756 KB |
Output is correct |
65 |
Correct |
133 ms |
12652 KB |
Output is correct |
66 |
Correct |
135 ms |
12652 KB |
Output is correct |
67 |
Correct |
133 ms |
12524 KB |
Output is correct |
68 |
Correct |
95 ms |
12652 KB |
Output is correct |
69 |
Correct |
92 ms |
12524 KB |
Output is correct |
70 |
Correct |
91 ms |
12396 KB |
Output is correct |