-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnarrowartgallery.cpp
More file actions
64 lines (56 loc) · 1.78 KB
/
Copy pathnarrowartgallery.cpp
File metadata and controls
64 lines (56 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int LEFT = 0;
const int RIGHT = 1;
const int NEITHER = 2;
int main() {
cin.tie(0)->sync_with_stdio(0);
while (true) {
int n, k;
cin >> n >> k;
if (n == 0 && k == 0)
break;
ll sum = 0;
vll left(n), right(n);
for (int i = 0; i < n; i++) {
cin >> left[i] >> right[i];
sum += left[i] + right[i];
}
// dp[i][j][k] stores the min considering the first i, having taken j
vector<vvll> dp(n, vvll(k + 1, vll(3, INT_MAX)));
dp[0][0][NEITHER] = 0;
if (k > 0) {
dp[0][1][LEFT] = left[0];
dp[0][1][RIGHT] = right[0];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < k + 1; j++) {
dp[i][j][NEITHER] =
min(dp[i - 1][j][NEITHER],
min(dp[i - 1][j][LEFT], dp[i - 1][j][RIGHT]));
if (j > 0) {
dp[i][j][LEFT] =
min(dp[i - 1][j - 1][LEFT], dp[i - 1][j - 1][NEITHER]) +
left[i];
dp[i][j][RIGHT] = min(dp[i - 1][j - 1][RIGHT],
dp[i - 1][j - 1][NEITHER]) +
right[i];
}
}
}
ll min_cost = min(dp[n - 1][k][NEITHER],
min(dp[n - 1][k][LEFT], dp[n - 1][k][RIGHT]));
ll ans = sum - min_cost;
cout << ans << endl;
}
}