Submission #418074

# Submission time Handle Problem Language Result Execution time Memory
418074 2021-06-05T03:50:20 Z Namnamseo JOI15_cardgame2 (JOI15_cardgame2) C++17
100 / 100
1675 ms 519772 KB
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;
const int maxn = int(5e2) + 10;

int n;

int c[maxn];
int a[maxn];
int v[maxn];

bool ok(int i, int j) { return !i || !j || c[i]==c[j] || a[i]==a[j]; }

int f1(int start, int last);
int f2(int s2, int soth);
int f3(int s1, int soth, int last);
int f4(int sa, int sb, int soth);

// [*]
int f1(int start, int last) {
	static int dp[maxn][maxn]; static bitset<maxn> vis[maxn];
	int &ret = dp[start][last];
	if (vis[start][last]) return ret;
	vis[start][last] = 1;

	int i = start;
	if (i <= n && ok(last, i)) {
		ret = max(ret, v[i] + f1(start+1, i));
	}

	i = start + 2;
	if (i <= n && ok(last, i)) {
		ret = max(ret, v[i] + f2(start, i+1));
	}

	return ret;
}

// 2, *, [*]
// last: soth-1
int f2(int s2, int soth) {
	static int dp[maxn][maxn]; static bitset<maxn> vis[maxn];
	int &ret = dp[s2][soth];
	if (vis[s2][soth]) return ret;
	vis[s2][soth] = 1;
	const int last = soth-1;

	int i = s2;
	if (ok(last, i)) {
		ret = max(ret, v[i] + f3(i+1, soth, i));
	}

	i = soth;
	if (i <= n && ok(last, i)) {
		ret = max(ret, v[i] + f2(s2, i+1));
	}

	return ret;
}

// 1, *, [*]
int f3(int s1, int soth, int last) {
	static int dp[maxn][maxn][maxn]; static bitset<maxn> vis[maxn][maxn];
	int &ret = dp[s1][soth][last];
	if (vis[s1][soth][last]) return ret;
	vis[s1][soth][last] = 1;

	int i = s1;
	if (ok(last, i)) {
		ret = max(ret, v[i] + f1(soth, i));
	}

	i = soth+1;
	if (i <= n && ok(last, i)) {
		ret = max(ret, v[i] + f4(s1, soth, i+1));
	}

	return ret;
}

// 1, *, 1, *, [*]
// last: soth-1
int f4(int sa, int sb, int soth) {
	static int dp[maxn][maxn][maxn]; static bitset<maxn> vis[maxn][maxn];
	int &ret = dp[sa][sb][soth];
	if (vis[sa][sb][soth]) return ret;
	vis[sa][sb][soth] = 1;
	const int last = soth-1;

	int i = sa;
	if (ok(last, i)) {
		ret = max(ret, v[i] + f3(sb, soth, i));
	}

	i = soth;
	if (i <= n && ok(last, i)) {
		ret = max(ret, v[i] + f4(sa, sb, i+1));
	}

	return ret;
}

int main()
{
	cin.tie(0)->sync_with_stdio(0);

	cin >> n;
	for (int i=1; i<=n; ++i) cin >> c[i] >> a[i] >> v[i];

	cout << f1(1, 0) << '\n';

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 844 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 452 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 1100 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 1 ms 1356 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 844 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 452 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 1100 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 1 ms 1356 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 972 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 1484 KB Output is correct
29 Correct 1 ms 968 KB Output is correct
30 Correct 1 ms 328 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 3 ms 3916 KB Output is correct
33 Correct 2 ms 2892 KB Output is correct
34 Correct 3 ms 3276 KB Output is correct
35 Correct 2 ms 2108 KB Output is correct
36 Correct 2 ms 2252 KB Output is correct
37 Correct 1 ms 1356 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 1 ms 1228 KB Output is correct
40 Correct 1 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 844 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 452 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 1100 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 1 ms 1356 KB Output is correct
18 Correct 1 ms 716 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 972 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 1484 KB Output is correct
29 Correct 1 ms 968 KB Output is correct
30 Correct 1 ms 328 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 3 ms 3916 KB Output is correct
33 Correct 2 ms 2892 KB Output is correct
34 Correct 3 ms 3276 KB Output is correct
35 Correct 2 ms 2108 KB Output is correct
36 Correct 2 ms 2252 KB Output is correct
37 Correct 1 ms 1356 KB Output is correct
38 Correct 1 ms 716 KB Output is correct
39 Correct 1 ms 1228 KB Output is correct
40 Correct 1 ms 1612 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 5 ms 7628 KB Output is correct
45 Correct 4 ms 6476 KB Output is correct
46 Correct 1 ms 972 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 6 ms 8272 KB Output is correct
49 Correct 1 ms 972 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 7 ms 10188 KB Output is correct
52 Correct 21 ms 27740 KB Output is correct
53 Correct 12 ms 16128 KB Output is correct
54 Correct 5 ms 7884 KB Output is correct
55 Correct 3 ms 4428 KB Output is correct
56 Correct 1 ms 844 KB Output is correct
57 Correct 14 ms 20684 KB Output is correct
58 Correct 9 ms 13132 KB Output is correct
59 Correct 8 ms 11468 KB Output is correct
60 Correct 4 ms 6092 KB Output is correct
61 Correct 4 ms 5708 KB Output is correct
62 Correct 3 ms 4812 KB Output is correct
63 Correct 1675 ms 519772 KB Output is correct
64 Correct 935 ms 347636 KB Output is correct
65 Correct 625 ms 256404 KB Output is correct
66 Correct 380 ms 180548 KB Output is correct
67 Correct 451 ms 201200 KB Output is correct