Submission #439033

# Submission time Handle Problem Language Result Execution time Memory
439033 2021-06-29T05:49:47 Z meatrow Visiting Singapore (NOI20_visitingsingapore) C++17
100 / 100
756 ms 584 KB
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
 
const int MOD = 998244353;
 
ll binpow(ll a, ll p, int mod = MOD) {
    ll res = 1;
    while (p) {
        if (p & 1) {
            (res *= a) %= mod;
        }
        p >>= 1;
        (a *= a) %= mod;
    }
    return res;
}
 
ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

const int INF = -1e9;
using state = array<array<int, 2>, 2>;
const state INIT = state{array<int, 2>{INF, INF}, array<int, 2>{INF, INF}};

void solve() {
    int K, n, m;
    int A, B;
    cin >> K >> n >> m >> A >> B;
    vector<int> v(K + 1);
    for (int i = 1; i <= K; i++) {
        cin >> v[i];
    }
    vector<int> s(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
    }
    vector<int> t(m + 1);
    for (int i = 1; i <= m; i++) {
        cin >> t[i];
    }
    auto chmax = [&](int& cur, int val) {
        if (val < INF / 2) return;
        cur = max(cur, val);
    };

    int ans = A + B * m;
    vector<state> dp(m + 1, INIT);
    for (int i = 1; i <= n; i++) {
        vector<state> nxt(m + 1, INIT);
        for (int j = 0; j <= m; j++) {
            if (j && s[i] == t[j]) {
                chmax(nxt[j][1][1], v[s[i]] + A * (j > 1) + B * (j - 1));
            }
            for (int f = 0; f < 2; f++) {
                for (int g = 0; g < 2; g++) {
                    chmax(nxt[j][0][g], dp[j][f][g] + A * f + B);
                    if (j) chmax(nxt[j][f][0], nxt[j - 1][f][g] + A * g + B);
                    if (j && s[i] == t[j]) {
                        chmax(nxt[j][1][1], dp[j - 1][f][g] + v[s[i]]);
                    }
                }
            }
            for (int f = 0; f < 2; f++) {
                for (int g = 0; g < 2; g++) {
                    chmax(ans, nxt[j][f][g] + A * (m - j > 0) + B * (m - j));
                }
            }
        }
        dp = move(nxt);
    }
    cout << ans << endl;
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    for (int tc = 1; tc <= T; tc++) {
        // cout << "Case #" << tc << ": ";
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 28 ms 344 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 7 ms 320 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 3 ms 204 KB Output is correct
9 Correct 14 ms 336 KB Output is correct
10 Correct 29 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 7 ms 348 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 3 ms 204 KB Output is correct
6 Correct 21 ms 344 KB Output is correct
7 Correct 14 ms 332 KB Output is correct
8 Correct 20 ms 348 KB Output is correct
9 Correct 3 ms 204 KB Output is correct
10 Correct 29 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 332 KB Output is correct
2 Correct 22 ms 332 KB Output is correct
3 Correct 171 ms 396 KB Output is correct
4 Correct 463 ms 464 KB Output is correct
5 Correct 133 ms 332 KB Output is correct
6 Correct 177 ms 460 KB Output is correct
7 Correct 334 ms 464 KB Output is correct
8 Correct 114 ms 332 KB Output is correct
9 Correct 232 ms 460 KB Output is correct
10 Correct 555 ms 460 KB Output is correct
11 Correct 540 ms 580 KB Output is correct
12 Correct 529 ms 532 KB Output is correct
13 Correct 484 ms 512 KB Output is correct
14 Correct 702 ms 516 KB Output is correct
15 Correct 691 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 332 KB Output is correct
2 Correct 22 ms 332 KB Output is correct
3 Correct 171 ms 396 KB Output is correct
4 Correct 463 ms 464 KB Output is correct
5 Correct 133 ms 332 KB Output is correct
6 Correct 177 ms 460 KB Output is correct
7 Correct 334 ms 464 KB Output is correct
8 Correct 114 ms 332 KB Output is correct
9 Correct 232 ms 460 KB Output is correct
10 Correct 555 ms 460 KB Output is correct
11 Correct 540 ms 580 KB Output is correct
12 Correct 529 ms 532 KB Output is correct
13 Correct 484 ms 512 KB Output is correct
14 Correct 702 ms 516 KB Output is correct
15 Correct 691 ms 512 KB Output is correct
16 Correct 281 ms 464 KB Output is correct
17 Correct 334 ms 584 KB Output is correct
18 Correct 297 ms 332 KB Output is correct
19 Correct 72 ms 412 KB Output is correct
20 Correct 222 ms 436 KB Output is correct
21 Correct 13 ms 336 KB Output is correct
22 Correct 76 ms 332 KB Output is correct
23 Correct 140 ms 460 KB Output is correct
24 Correct 52 ms 452 KB Output is correct
25 Correct 540 ms 528 KB Output is correct
26 Correct 580 ms 460 KB Output is correct
27 Correct 517 ms 524 KB Output is correct
28 Correct 496 ms 516 KB Output is correct
29 Correct 702 ms 580 KB Output is correct
30 Correct 756 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 332 KB Output is correct
2 Correct 22 ms 332 KB Output is correct
3 Correct 171 ms 396 KB Output is correct
4 Correct 463 ms 464 KB Output is correct
5 Correct 133 ms 332 KB Output is correct
6 Correct 177 ms 460 KB Output is correct
7 Correct 334 ms 464 KB Output is correct
8 Correct 114 ms 332 KB Output is correct
9 Correct 232 ms 460 KB Output is correct
10 Correct 555 ms 460 KB Output is correct
11 Correct 540 ms 580 KB Output is correct
12 Correct 529 ms 532 KB Output is correct
13 Correct 484 ms 512 KB Output is correct
14 Correct 702 ms 516 KB Output is correct
15 Correct 691 ms 512 KB Output is correct
16 Correct 442 ms 488 KB Output is correct
17 Correct 216 ms 332 KB Output is correct
18 Correct 65 ms 332 KB Output is correct
19 Correct 17 ms 352 KB Output is correct
20 Correct 9 ms 332 KB Output is correct
21 Correct 12 ms 484 KB Output is correct
22 Correct 211 ms 460 KB Output is correct
23 Correct 39 ms 332 KB Output is correct
24 Correct 361 ms 460 KB Output is correct
25 Correct 525 ms 532 KB Output is correct
26 Correct 561 ms 580 KB Output is correct
27 Correct 520 ms 524 KB Output is correct
28 Correct 478 ms 580 KB Output is correct
29 Correct 713 ms 580 KB Output is correct
30 Correct 702 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 28 ms 344 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 7 ms 320 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 3 ms 204 KB Output is correct
9 Correct 14 ms 336 KB Output is correct
10 Correct 29 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 7 ms 348 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 3 ms 204 KB Output is correct
16 Correct 21 ms 344 KB Output is correct
17 Correct 14 ms 332 KB Output is correct
18 Correct 20 ms 348 KB Output is correct
19 Correct 3 ms 204 KB Output is correct
20 Correct 29 ms 332 KB Output is correct
21 Correct 51 ms 332 KB Output is correct
22 Correct 22 ms 332 KB Output is correct
23 Correct 171 ms 396 KB Output is correct
24 Correct 463 ms 464 KB Output is correct
25 Correct 133 ms 332 KB Output is correct
26 Correct 177 ms 460 KB Output is correct
27 Correct 334 ms 464 KB Output is correct
28 Correct 114 ms 332 KB Output is correct
29 Correct 232 ms 460 KB Output is correct
30 Correct 555 ms 460 KB Output is correct
31 Correct 540 ms 580 KB Output is correct
32 Correct 529 ms 532 KB Output is correct
33 Correct 484 ms 512 KB Output is correct
34 Correct 702 ms 516 KB Output is correct
35 Correct 691 ms 512 KB Output is correct
36 Correct 281 ms 464 KB Output is correct
37 Correct 334 ms 584 KB Output is correct
38 Correct 297 ms 332 KB Output is correct
39 Correct 72 ms 412 KB Output is correct
40 Correct 222 ms 436 KB Output is correct
41 Correct 13 ms 336 KB Output is correct
42 Correct 76 ms 332 KB Output is correct
43 Correct 140 ms 460 KB Output is correct
44 Correct 52 ms 452 KB Output is correct
45 Correct 540 ms 528 KB Output is correct
46 Correct 580 ms 460 KB Output is correct
47 Correct 517 ms 524 KB Output is correct
48 Correct 496 ms 516 KB Output is correct
49 Correct 702 ms 580 KB Output is correct
50 Correct 756 ms 580 KB Output is correct
51 Correct 442 ms 488 KB Output is correct
52 Correct 216 ms 332 KB Output is correct
53 Correct 65 ms 332 KB Output is correct
54 Correct 17 ms 352 KB Output is correct
55 Correct 9 ms 332 KB Output is correct
56 Correct 12 ms 484 KB Output is correct
57 Correct 211 ms 460 KB Output is correct
58 Correct 39 ms 332 KB Output is correct
59 Correct 361 ms 460 KB Output is correct
60 Correct 525 ms 532 KB Output is correct
61 Correct 561 ms 580 KB Output is correct
62 Correct 520 ms 524 KB Output is correct
63 Correct 478 ms 580 KB Output is correct
64 Correct 713 ms 580 KB Output is correct
65 Correct 702 ms 580 KB Output is correct
66 Correct 1 ms 204 KB Output is correct
67 Correct 1 ms 204 KB Output is correct
68 Correct 1 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 204 KB Output is correct
75 Correct 1 ms 204 KB Output is correct
76 Correct 2 ms 204 KB Output is correct
77 Correct 1 ms 204 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 1 ms 204 KB Output is correct
81 Correct 427 ms 496 KB Output is correct
82 Correct 103 ms 452 KB Output is correct
83 Correct 193 ms 460 KB Output is correct
84 Correct 108 ms 436 KB Output is correct
85 Correct 175 ms 332 KB Output is correct
86 Correct 142 ms 404 KB Output is correct
87 Correct 27 ms 332 KB Output is correct
88 Correct 16 ms 348 KB Output is correct
89 Correct 59 ms 380 KB Output is correct
90 Correct 642 ms 508 KB Output is correct
91 Correct 523 ms 528 KB Output is correct
92 Correct 521 ms 524 KB Output is correct
93 Correct 528 ms 532 KB Output is correct
94 Correct 528 ms 520 KB Output is correct
95 Correct 481 ms 512 KB Output is correct
96 Correct 517 ms 460 KB Output is correct
97 Correct 515 ms 520 KB Output is correct
98 Correct 743 ms 580 KB Output is correct
99 Correct 747 ms 504 KB Output is correct
100 Correct 753 ms 508 KB Output is correct