Submission #895374

# Submission time Handle Problem Language Result Execution time Memory
895374 2023-12-29T20:03:25 Z MilosMilutinovic JOI15_cardgame2 (JOI15_cardgame2) C++14
100 / 100
2263 ms 503632 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX = 505;

int dp[MAX][MAX][MAX];

int ckmx(int& a, int b) {
  a = max(a, b);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> c(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> c[i] >> v[i];
  }
  vector<vector<bool>> ok(n, vector<bool>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      ok[i][j] = (a[i] == a[j] || c[i] == c[j]);
    }
  }
  const int inf = (int) 1e9;
  for (int i = 0; i < n + 2; i++) {
    for (int j = 0; j < n + 2; j++) {
      for (int k = 0; k < n + 2; k++) {
        dp[i][j][k] = -inf;
      }
    }
  }
  function<int(int, int, int, int)> Solve = [&](int x, int y, int z, int f) {
    if (x >= n) {
      return 0;
    }
    if (dp[x][y][f] != -inf) {
      return dp[x][y][f];
    }
    /*if (was[x][z][f]) {
      return dp[x][z][f];
    }
    was[x][z][f] = true;*/
    dp[x][y][f] = 0;
    if (x < n && ok[f][x]) {
      dp[x][y][f] = max(dp[x][y][f], Solve(y, z, z + 1, x) + v[x]);
    }
    if (z < n && ok[f][z]) {
      dp[x][y][f] = max(dp[x][y][f], Solve(x, y, z + 1, z) + v[z]);
    }
    return dp[x][y][f];
  };
  int res = Solve(1, 2, 3, 0) + v[0];
  if (n >= 3) {
    res = max(res, Solve(0, 1, 3, 2) + v[2]);
  }
  cout << res << '\n';
  return 0;
}

Compilation message

cardgame2.cpp: In function 'int ckmx(int&, int)':
cardgame2.cpp:11:1: warning: no return statement in function returning non-void [-Wreturn-type]
   11 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 3 ms 23036 KB Output is correct
8 Correct 3 ms 22876 KB Output is correct
9 Correct 3 ms 22872 KB Output is correct
10 Correct 3 ms 22876 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 3 ms 23044 KB Output is correct
13 Correct 3 ms 22876 KB Output is correct
14 Correct 3 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 3 ms 22876 KB Output is correct
17 Correct 3 ms 22876 KB Output is correct
18 Correct 3 ms 22876 KB Output is correct
19 Correct 3 ms 22872 KB Output is correct
20 Correct 3 ms 22876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 3 ms 23036 KB Output is correct
8 Correct 3 ms 22876 KB Output is correct
9 Correct 3 ms 22872 KB Output is correct
10 Correct 3 ms 22876 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 3 ms 23044 KB Output is correct
13 Correct 3 ms 22876 KB Output is correct
14 Correct 3 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 3 ms 22876 KB Output is correct
17 Correct 3 ms 22876 KB Output is correct
18 Correct 3 ms 22876 KB Output is correct
19 Correct 3 ms 22872 KB Output is correct
20 Correct 3 ms 22876 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 4 ms 33116 KB Output is correct
25 Correct 8 ms 51804 KB Output is correct
26 Correct 5 ms 51804 KB Output is correct
27 Correct 6 ms 51804 KB Output is correct
28 Correct 6 ms 51804 KB Output is correct
29 Correct 6 ms 51804 KB Output is correct
30 Correct 6 ms 51804 KB Output is correct
31 Correct 6 ms 51660 KB Output is correct
32 Correct 6 ms 52216 KB Output is correct
33 Correct 6 ms 51804 KB Output is correct
34 Correct 6 ms 51804 KB Output is correct
35 Correct 6 ms 51804 KB Output is correct
36 Correct 6 ms 51804 KB Output is correct
37 Correct 6 ms 51804 KB Output is correct
38 Correct 5 ms 51664 KB Output is correct
39 Correct 7 ms 51804 KB Output is correct
40 Correct 6 ms 51804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 3 ms 23036 KB Output is correct
8 Correct 3 ms 22876 KB Output is correct
9 Correct 3 ms 22872 KB Output is correct
10 Correct 3 ms 22876 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 3 ms 23044 KB Output is correct
13 Correct 3 ms 22876 KB Output is correct
14 Correct 3 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 3 ms 22876 KB Output is correct
17 Correct 3 ms 22876 KB Output is correct
18 Correct 3 ms 22876 KB Output is correct
19 Correct 3 ms 22872 KB Output is correct
20 Correct 3 ms 22876 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 4 ms 33116 KB Output is correct
25 Correct 8 ms 51804 KB Output is correct
26 Correct 5 ms 51804 KB Output is correct
27 Correct 6 ms 51804 KB Output is correct
28 Correct 6 ms 51804 KB Output is correct
29 Correct 6 ms 51804 KB Output is correct
30 Correct 6 ms 51804 KB Output is correct
31 Correct 6 ms 51660 KB Output is correct
32 Correct 6 ms 52216 KB Output is correct
33 Correct 6 ms 51804 KB Output is correct
34 Correct 6 ms 51804 KB Output is correct
35 Correct 6 ms 51804 KB Output is correct
36 Correct 6 ms 51804 KB Output is correct
37 Correct 6 ms 51804 KB Output is correct
38 Correct 5 ms 51664 KB Output is correct
39 Correct 7 ms 51804 KB Output is correct
40 Correct 6 ms 51804 KB Output is correct
41 Correct 1 ms 2648 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4700 KB Output is correct
44 Correct 174 ms 501308 KB Output is correct
45 Correct 134 ms 501388 KB Output is correct
46 Correct 109 ms 501320 KB Output is correct
47 Correct 102 ms 501448 KB Output is correct
48 Correct 123 ms 501328 KB Output is correct
49 Correct 112 ms 501336 KB Output is correct
50 Correct 95 ms 501508 KB Output is correct
51 Correct 103 ms 501644 KB Output is correct
52 Correct 102 ms 501444 KB Output is correct
53 Correct 111 ms 501328 KB Output is correct
54 Correct 110 ms 503352 KB Output is correct
55 Correct 119 ms 501544 KB Output is correct
56 Correct 107 ms 501332 KB Output is correct
57 Correct 110 ms 501584 KB Output is correct
58 Correct 106 ms 501360 KB Output is correct
59 Correct 106 ms 501516 KB Output is correct
60 Correct 107 ms 501548 KB Output is correct
61 Correct 94 ms 503356 KB Output is correct
62 Correct 97 ms 501456 KB Output is correct
63 Correct 2263 ms 501608 KB Output is correct
64 Correct 1161 ms 503632 KB Output is correct
65 Correct 427 ms 503376 KB Output is correct
66 Correct 297 ms 503512 KB Output is correct
67 Correct 320 ms 503324 KB Output is correct