-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnarrowartgallery.cpp
48 lines (40 loc) · 1.05 KB
/
narrowartgallery.cpp
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
/*
dynamic programming
difficulty: easy
date: 08/Jul/2021
problem: narrow art gallery
by: @brpapa
*/
#include <bits/stdc++.h>
using namespace std;
#define min3(a, b, c) ((a) < (b) ? min(a, c) : min(b, c))
typedef long long ll;
const int INF = 1 << 30;
int N;
int K;
vector<pair<int, int>> rooms;
enum { LEFT, RIGHT, NONE };
int memo[202][202][3];
int dp(int i, int k, int prev_selected) {
if (k == K) return 0;
if (i == N && k < K) return INF;
int &ans = memo[i][k][prev_selected];
if (ans != -1) return ans;
ans = dp(i+1, k, NONE);
if (prev_selected != RIGHT) ans = min(ans, rooms[i].first + dp(i+1, k+1, LEFT));
if (prev_selected != LEFT) ans = min(ans, rooms[i].second + dp(i+1, k+1, RIGHT));
return ans;
}
int main() {
while (cin >> N >> K && (N || K)) {
rooms.resize(N);
memset(memo, -1, sizeof memo);
int sum = 0;
for (auto &room : rooms) {
cin >> room.first >> room.second;
sum += room.first + room.second;
}
cout << sum - dp(0, 0, NONE) << endl;
}
return 0;
}