//2019 NOI (National Olympiad of Singapore)
//Lagrangian Relaxation algorithm
#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define pii pair<int, int>
#define f first
#define s second
const int maxn = 3e5 + 5;
pii dp[maxn][2];
int A[maxn];
int N, K;
pii check(int lambda)
{
dp[0][0] = {0, 0};
dp[0][1] = {A[0] - lambda, 1};
for (int i = 1; i < N; i++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = max(make_pair(dp[i - 1][1].first + A[i], dp[i - 1][1].s),
make_pair(dp[i - 1][0].f - lambda + A[i], dp[i - 1][0].s + 1));
//cout << i << " " << dp[i][0].f << " " << dp[i][1].f << endl;
}
return max(dp[N - 1][0], dp[N - 1][1]);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
for (int i = 0; i < N; i++)
cin >> A[i];
//cout << check(2).f << " bum\n";
//find lambda
int lo = 0, hi = 1e18;
while(lo < hi)
{
int mid = (hi + lo + 1) / 2;
//cout << mid <<endl;
auto smth = check(mid);
//cout << smth.f << " " << smth.s <<endl;
if (smth.s >= K)
lo = mid;
else
hi = mid - 1;
}
//cout << lo <<endl;
cout << check(lo).f + lo * K;
}
/*
6 1
1 -2 3 -1 5 -6
ans: 7
6 2
1 2 3 -10 5 6
ans: 17
6 4
-1 -2 -1 0 -5 -1
ans: 0
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
14676 KB |
Output is correct |
2 |
Correct |
72 ms |
14676 KB |
Output is correct |
3 |
Correct |
73 ms |
15052 KB |
Output is correct |
4 |
Correct |
74 ms |
14960 KB |
Output is correct |
5 |
Correct |
75 ms |
14900 KB |
Output is correct |
6 |
Correct |
71 ms |
14672 KB |
Output is correct |
7 |
Correct |
72 ms |
14696 KB |
Output is correct |
8 |
Correct |
87 ms |
14936 KB |
Output is correct |
9 |
Correct |
71 ms |
14672 KB |
Output is correct |
10 |
Correct |
72 ms |
14700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
12892 KB |
Output is correct |
2 |
Correct |
66 ms |
13292 KB |
Output is correct |
3 |
Correct |
66 ms |
12888 KB |
Output is correct |
4 |
Correct |
65 ms |
13148 KB |
Output is correct |
5 |
Correct |
75 ms |
14704 KB |
Output is correct |
6 |
Correct |
62 ms |
12888 KB |
Output is correct |
7 |
Correct |
68 ms |
13144 KB |
Output is correct |
8 |
Correct |
79 ms |
14984 KB |
Output is correct |
9 |
Correct |
74 ms |
14664 KB |
Output is correct |
10 |
Correct |
67 ms |
13032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
15088 KB |
Output is correct |
2 |
Correct |
95 ms |
14928 KB |
Output is correct |
3 |
Correct |
95 ms |
15056 KB |
Output is correct |
4 |
Correct |
109 ms |
14944 KB |
Output is correct |
5 |
Correct |
96 ms |
14960 KB |
Output is correct |
6 |
Correct |
96 ms |
15132 KB |
Output is correct |
7 |
Correct |
96 ms |
15176 KB |
Output is correct |
8 |
Correct |
98 ms |
14964 KB |
Output is correct |
9 |
Correct |
106 ms |
15208 KB |
Output is correct |
10 |
Correct |
94 ms |
15192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2512 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2512 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2508 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2512 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2516 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2508 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
2 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
14676 KB |
Output is correct |
2 |
Correct |
72 ms |
14676 KB |
Output is correct |
3 |
Correct |
73 ms |
15052 KB |
Output is correct |
4 |
Correct |
74 ms |
14960 KB |
Output is correct |
5 |
Correct |
75 ms |
14900 KB |
Output is correct |
6 |
Correct |
71 ms |
14672 KB |
Output is correct |
7 |
Correct |
72 ms |
14696 KB |
Output is correct |
8 |
Correct |
87 ms |
14936 KB |
Output is correct |
9 |
Correct |
71 ms |
14672 KB |
Output is correct |
10 |
Correct |
72 ms |
14700 KB |
Output is correct |
11 |
Correct |
63 ms |
12892 KB |
Output is correct |
12 |
Correct |
66 ms |
13292 KB |
Output is correct |
13 |
Correct |
66 ms |
12888 KB |
Output is correct |
14 |
Correct |
65 ms |
13148 KB |
Output is correct |
15 |
Correct |
75 ms |
14704 KB |
Output is correct |
16 |
Correct |
62 ms |
12888 KB |
Output is correct |
17 |
Correct |
68 ms |
13144 KB |
Output is correct |
18 |
Correct |
79 ms |
14984 KB |
Output is correct |
19 |
Correct |
74 ms |
14664 KB |
Output is correct |
20 |
Correct |
67 ms |
13032 KB |
Output is correct |
21 |
Correct |
95 ms |
15088 KB |
Output is correct |
22 |
Correct |
95 ms |
14928 KB |
Output is correct |
23 |
Correct |
95 ms |
15056 KB |
Output is correct |
24 |
Correct |
109 ms |
14944 KB |
Output is correct |
25 |
Correct |
96 ms |
14960 KB |
Output is correct |
26 |
Correct |
96 ms |
15132 KB |
Output is correct |
27 |
Correct |
96 ms |
15176 KB |
Output is correct |
28 |
Correct |
98 ms |
14964 KB |
Output is correct |
29 |
Correct |
106 ms |
15208 KB |
Output is correct |
30 |
Correct |
94 ms |
15192 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2512 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2516 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2648 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2392 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2508 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2392 KB |
Output is correct |
56 |
Correct |
2 ms |
2392 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
2 ms |
2396 KB |
Output is correct |
61 |
Correct |
138 ms |
14812 KB |
Output is correct |
62 |
Correct |
130 ms |
15216 KB |
Output is correct |
63 |
Correct |
114 ms |
14900 KB |
Output is correct |
64 |
Correct |
137 ms |
15136 KB |
Output is correct |
65 |
Correct |
134 ms |
15080 KB |
Output is correct |
66 |
Correct |
144 ms |
15104 KB |
Output is correct |
67 |
Correct |
148 ms |
14912 KB |
Output is correct |
68 |
Correct |
96 ms |
15152 KB |
Output is correct |
69 |
Correct |
95 ms |
14672 KB |
Output is correct |
70 |
Correct |
98 ms |
14840 KB |
Output is correct |