Submission #693005

# Submission time Handle Problem Language Result Execution time Memory
693005 2023-02-02T08:43:48 Z saayan007 Visiting Singapore (NOI20_visitingsingapore) C++17
100 / 100
654 ms 848 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;

#define fur(i, a, b) for(ll i = a; i <= (ll) b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll) b; --i)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define nl "\n"

const ll inf = 1e10L;

ll k, n, m, a, b;
ll cost(ll y, ll x) {
    ll l, r;

    ll ans = 0;

    l = y%2, r = x%2;
    if(r == 0) {
        ans -= b;
        if(l == 1)
            ans -= a;
    }

    y /= 2;
    x /= 2;

    l = y%2, r = x%2;
    if(r == 0) {
        ans -= b;
        if(l == 1)
            ans -= a;
    }
    return ans;
}


int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> k >> n >> m >> a >> b;
    a = -a;
    b = -b;

    ll v[k + 1], s[n + 1], t[m + 1];

    fur(i, 1, k) {
        cin >> v[i];
    }
    fur(i, 1, n) {
        cin >> s[i];
    }
    fur(i, 1, m) {
        cin >> t[i];
    }

    ll dp[2][m + 1][4];
    fur(i, 0, 1)
        fur(j, 0, m)
            fur(x, 0, 3)
                dp[i][j][x] = -inf;

    ll res = -a -m*b;

    fur(i, 1, n) {
        ll cur = i % 2;
        ll prv = 1 - cur;

        fur(j, 1, m) {
            if(s[i] == t[j]) {
                fur(y, 0, 3) {
                    dp[cur][j][3] = max({dp[cur][j][3], dp[prv][j - 1][y] + v[t[j]] + cost(y, 3), v[t[j]] + (j == 1 ? 0 : - a - (j - 1) * b)});
                    res = max(res, dp[cur][j][3] + (j == m ? 0 : - a - b * (m - j)));
                }
            }

            dp[cur][j][0] = max({dp[cur][j][0], dp[prv][j][0] + cost(0, 0), dp[prv][j][2] + cost(2, 0), dp[cur][j - 1][0] + cost(0, 0), dp[cur][j - 1][1] + cost(1, 0)});
            fur(y, 0, 3) {
                dp[cur][j][0] = max(dp[cur][j][0], dp[prv][j - 1][y] + cost(y, 0));
            }
            dp[cur][j][1] = max({dp[cur][j][1], dp[prv][j][1] + cost(1, 1), dp[prv][j][3] + cost(3, 1)});
            dp[cur][j][2] = max({dp[cur][j][2], dp[cur][j - 1][2] + cost(2, 2), dp[cur][j - 1][3] + cost(3, 2)});
        }

        fur(j, 0, m) {
            fur(x, 0, 3) {
                dp[prv][j][x] = -inf;
            }
        }
    }
    cout << res << nl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 19 ms 384 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 6 ms 316 KB Output is correct
7 Correct 2 ms 324 KB Output is correct
8 Correct 3 ms 212 KB Output is correct
9 Correct 18 ms 368 KB Output is correct
10 Correct 43 ms 372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 9 ms 380 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 13 ms 320 KB Output is correct
7 Correct 11 ms 316 KB Output is correct
8 Correct 21 ms 340 KB Output is correct
9 Correct 3 ms 316 KB Output is correct
10 Correct 31 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 340 KB Output is correct
2 Correct 13 ms 340 KB Output is correct
3 Correct 114 ms 468 KB Output is correct
4 Correct 296 ms 596 KB Output is correct
5 Correct 84 ms 596 KB Output is correct
6 Correct 115 ms 596 KB Output is correct
7 Correct 245 ms 604 KB Output is correct
8 Correct 70 ms 460 KB Output is correct
9 Correct 144 ms 600 KB Output is correct
10 Correct 333 ms 724 KB Output is correct
11 Correct 374 ms 736 KB Output is correct
12 Correct 333 ms 728 KB Output is correct
13 Correct 349 ms 724 KB Output is correct
14 Correct 539 ms 716 KB Output is correct
15 Correct 557 ms 712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 340 KB Output is correct
2 Correct 13 ms 340 KB Output is correct
3 Correct 114 ms 468 KB Output is correct
4 Correct 296 ms 596 KB Output is correct
5 Correct 84 ms 596 KB Output is correct
6 Correct 115 ms 596 KB Output is correct
7 Correct 245 ms 604 KB Output is correct
8 Correct 70 ms 460 KB Output is correct
9 Correct 144 ms 600 KB Output is correct
10 Correct 333 ms 724 KB Output is correct
11 Correct 374 ms 736 KB Output is correct
12 Correct 333 ms 728 KB Output is correct
13 Correct 349 ms 724 KB Output is correct
14 Correct 539 ms 716 KB Output is correct
15 Correct 557 ms 712 KB Output is correct
16 Correct 177 ms 616 KB Output is correct
17 Correct 221 ms 600 KB Output is correct
18 Correct 194 ms 468 KB Output is correct
19 Correct 38 ms 520 KB Output is correct
20 Correct 134 ms 468 KB Output is correct
21 Correct 9 ms 468 KB Output is correct
22 Correct 44 ms 468 KB Output is correct
23 Correct 85 ms 468 KB Output is correct
24 Correct 33 ms 408 KB Output is correct
25 Correct 351 ms 724 KB Output is correct
26 Correct 347 ms 728 KB Output is correct
27 Correct 340 ms 724 KB Output is correct
28 Correct 344 ms 724 KB Output is correct
29 Correct 566 ms 724 KB Output is correct
30 Correct 553 ms 704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 340 KB Output is correct
2 Correct 13 ms 340 KB Output is correct
3 Correct 114 ms 468 KB Output is correct
4 Correct 296 ms 596 KB Output is correct
5 Correct 84 ms 596 KB Output is correct
6 Correct 115 ms 596 KB Output is correct
7 Correct 245 ms 604 KB Output is correct
8 Correct 70 ms 460 KB Output is correct
9 Correct 144 ms 600 KB Output is correct
10 Correct 333 ms 724 KB Output is correct
11 Correct 374 ms 736 KB Output is correct
12 Correct 333 ms 728 KB Output is correct
13 Correct 349 ms 724 KB Output is correct
14 Correct 539 ms 716 KB Output is correct
15 Correct 557 ms 712 KB Output is correct
16 Correct 287 ms 716 KB Output is correct
17 Correct 136 ms 468 KB Output is correct
18 Correct 40 ms 468 KB Output is correct
19 Correct 10 ms 340 KB Output is correct
20 Correct 6 ms 444 KB Output is correct
21 Correct 8 ms 584 KB Output is correct
22 Correct 131 ms 592 KB Output is correct
23 Correct 25 ms 324 KB Output is correct
24 Correct 239 ms 596 KB Output is correct
25 Correct 334 ms 740 KB Output is correct
26 Correct 349 ms 732 KB Output is correct
27 Correct 345 ms 736 KB Output is correct
28 Correct 342 ms 724 KB Output is correct
29 Correct 542 ms 704 KB Output is correct
30 Correct 626 ms 700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 19 ms 384 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 6 ms 316 KB Output is correct
7 Correct 2 ms 324 KB Output is correct
8 Correct 3 ms 212 KB Output is correct
9 Correct 18 ms 368 KB Output is correct
10 Correct 43 ms 372 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 9 ms 380 KB Output is correct
14 Correct 2 ms 212 KB Output is correct
15 Correct 5 ms 344 KB Output is correct
16 Correct 13 ms 320 KB Output is correct
17 Correct 11 ms 316 KB Output is correct
18 Correct 21 ms 340 KB Output is correct
19 Correct 3 ms 316 KB Output is correct
20 Correct 31 ms 340 KB Output is correct
21 Correct 35 ms 340 KB Output is correct
22 Correct 13 ms 340 KB Output is correct
23 Correct 114 ms 468 KB Output is correct
24 Correct 296 ms 596 KB Output is correct
25 Correct 84 ms 596 KB Output is correct
26 Correct 115 ms 596 KB Output is correct
27 Correct 245 ms 604 KB Output is correct
28 Correct 70 ms 460 KB Output is correct
29 Correct 144 ms 600 KB Output is correct
30 Correct 333 ms 724 KB Output is correct
31 Correct 374 ms 736 KB Output is correct
32 Correct 333 ms 728 KB Output is correct
33 Correct 349 ms 724 KB Output is correct
34 Correct 539 ms 716 KB Output is correct
35 Correct 557 ms 712 KB Output is correct
36 Correct 177 ms 616 KB Output is correct
37 Correct 221 ms 600 KB Output is correct
38 Correct 194 ms 468 KB Output is correct
39 Correct 38 ms 520 KB Output is correct
40 Correct 134 ms 468 KB Output is correct
41 Correct 9 ms 468 KB Output is correct
42 Correct 44 ms 468 KB Output is correct
43 Correct 85 ms 468 KB Output is correct
44 Correct 33 ms 408 KB Output is correct
45 Correct 351 ms 724 KB Output is correct
46 Correct 347 ms 728 KB Output is correct
47 Correct 340 ms 724 KB Output is correct
48 Correct 344 ms 724 KB Output is correct
49 Correct 566 ms 724 KB Output is correct
50 Correct 553 ms 704 KB Output is correct
51 Correct 287 ms 716 KB Output is correct
52 Correct 136 ms 468 KB Output is correct
53 Correct 40 ms 468 KB Output is correct
54 Correct 10 ms 340 KB Output is correct
55 Correct 6 ms 444 KB Output is correct
56 Correct 8 ms 584 KB Output is correct
57 Correct 131 ms 592 KB Output is correct
58 Correct 25 ms 324 KB Output is correct
59 Correct 239 ms 596 KB Output is correct
60 Correct 334 ms 740 KB Output is correct
61 Correct 349 ms 732 KB Output is correct
62 Correct 345 ms 736 KB Output is correct
63 Correct 342 ms 724 KB Output is correct
64 Correct 542 ms 704 KB Output is correct
65 Correct 626 ms 700 KB Output is correct
66 Correct 1 ms 212 KB Output is correct
67 Correct 1 ms 268 KB Output is correct
68 Correct 0 ms 212 KB Output is correct
69 Correct 1 ms 212 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 324 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 1 ms 212 KB Output is correct
75 Correct 1 ms 316 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 1 ms 320 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 282 ms 672 KB Output is correct
82 Correct 78 ms 468 KB Output is correct
83 Correct 129 ms 604 KB Output is correct
84 Correct 65 ms 580 KB Output is correct
85 Correct 111 ms 468 KB Output is correct
86 Correct 92 ms 488 KB Output is correct
87 Correct 16 ms 432 KB Output is correct
88 Correct 10 ms 380 KB Output is correct
89 Correct 39 ms 340 KB Output is correct
90 Correct 377 ms 724 KB Output is correct
91 Correct 343 ms 720 KB Output is correct
92 Correct 336 ms 724 KB Output is correct
93 Correct 347 ms 732 KB Output is correct
94 Correct 353 ms 724 KB Output is correct
95 Correct 334 ms 724 KB Output is correct
96 Correct 351 ms 712 KB Output is correct
97 Correct 359 ms 848 KB Output is correct
98 Correct 654 ms 704 KB Output is correct
99 Correct 564 ms 724 KB Output is correct
100 Correct 556 ms 704 KB Output is correct