forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1217-neighbour_house_II.cpp
70 lines (52 loc) · 1.07 KB
/
1217-neighbour_house_II.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[1100], n ;
ll dp[1100][2][2];
ll recurse(ll curr, ll taken, ll first)
{
if (curr == n)
return 0;
ll ans = 0;
if (curr == n - 1) {
if (taken == 0) {
return 0;
} else {
if (first == 0) {
return arr[curr];
} else {
return 0;
}
}
}
if (dp[curr][taken][first])
return dp[curr][taken][first];
if (taken == 0) {
ans = max(ans, recurse(curr + 1, 1, first) );
ans = max(ans, recurse(curr + 1, 0, first) );
} else {
ans = max(ans, arr[curr] + recurse(curr + 1, 0, first) );
}
return dp[curr][taken][first] = ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/Documents/sameer/input.sam", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
ll i, j, k, m, t, cont, a, b;
cin >> t;
ll cases = t;
while (t--) {
memset(dp, 0, sizeof dp);
cin >> n ;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
ll ans = max( recurse(0, 1, 1), recurse (0, 0, 0));
cout << "Case " << cases - t << ": " << ans << endl;
}
return 0;
}