Submission #555769

# Submission time Handle Problem Language Result Execution time Memory
555769 2022-05-01T13:51:46 Z AriaH Copy and Paste 3 (JOI22_copypaste3) C++17
62 / 100
549 ms 49740 KB
/* Im the best and i work like that */

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;

#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;

const int N = 2510;
const int LOG = 20;
const ll mod = 1e9 + 21;
const ll base = 5e4 + 21;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;

ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }

mt19937 rng(time(nullptr));

int n, par[N];

ll A, B, C, Hsh[N], P[N], dp[N][N]; /// dp -> how much we can save

string s;

inline ll get(ll l, ll r)
{
    return (Hsh[r] - (Hsh[l - 1] * P[r - l + 1] % mod) + mod) % mod;
}

int main()
{
	fast_io;
    for(int i = P[0] = 1; i < N; i ++) { P[i] = P[i - 1] * base % mod; }
    cin >> n >> s >> A >> B >> C;
    s = "." + s;
    for(int i = 1; i <= n; i ++) { Hsh[i] = (Hsh[i - 1] * base + s[i]) % mod; }
    for(int len = 1; len <= n; len ++)
    {
        memset(par, -1, sizeof par);
        vector < pll > vec;
        for(int i = len; i <= n; i ++)
        {
            vec.push_back(Mp(get(i - len + 1, i), i));
        }
        for(int i = 1; i <= n; i ++)
        {
            dp[i][i + len - 1] = max({dp[i][i + len - 1], dp[i + 1][i + len - 1], dp[i][i + len - 2]});
        }
        sort(all(vec));
        for(int i = len; i <= n; i ++)
        {
            ll cu = get(i - len + 1, i);
            int id = lower_bound(all(vec), Mp(cu, 1ll * i + len)) - vec.begin();
            if(id < SZ(vec) && vec[id].F == cu)
            {
                par[i] = vec[id].S;
            }
        }
        for(int i = len; i <= n; i ++)
        {
            int nxt = par[i], cnt = 1;
            ll cu = dp[i - len + 1][i];
            while(nxt != -1)
            {
                cnt ++;
                dp[i - len + 1][nxt] = max(dp[i - len + 1][nxt], cu + 1ll * len * (cnt - 1) * A - B - 1ll * cnt * C);
                nxt = par[nxt];
            }
        }
    }
    cout << 1ll * n * A - dp[1][n];
	return 0;
}

/* check corner case(n = 1?), watch for negetive index or overflow */
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 219 ms 33928 KB Output is correct
4 Correct 265 ms 38896 KB Output is correct
5 Correct 349 ms 42572 KB Output is correct
6 Correct 365 ms 45896 KB Output is correct
7 Correct 446 ms 49484 KB Output is correct
8 Correct 436 ms 49580 KB Output is correct
9 Correct 422 ms 49580 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 356 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 356 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 2 ms 1364 KB Output is correct
42 Correct 3 ms 1364 KB Output is correct
43 Correct 3 ms 1380 KB Output is correct
44 Correct 3 ms 1492 KB Output is correct
45 Correct 3 ms 1364 KB Output is correct
46 Correct 3 ms 1364 KB Output is correct
47 Correct 4 ms 1364 KB Output is correct
48 Correct 3 ms 1364 KB Output is correct
49 Correct 4 ms 1492 KB Output is correct
50 Correct 3 ms 1492 KB Output is correct
51 Correct 4 ms 1492 KB Output is correct
52 Correct 4 ms 1380 KB Output is correct
53 Correct 3 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 488 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 356 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 2 ms 1364 KB Output is correct
42 Correct 3 ms 1364 KB Output is correct
43 Correct 3 ms 1380 KB Output is correct
44 Correct 3 ms 1492 KB Output is correct
45 Correct 3 ms 1364 KB Output is correct
46 Correct 3 ms 1364 KB Output is correct
47 Correct 4 ms 1364 KB Output is correct
48 Correct 3 ms 1364 KB Output is correct
49 Correct 4 ms 1492 KB Output is correct
50 Correct 3 ms 1492 KB Output is correct
51 Correct 4 ms 1492 KB Output is correct
52 Correct 4 ms 1380 KB Output is correct
53 Correct 3 ms 1492 KB Output is correct
54 Correct 16 ms 3660 KB Output is correct
55 Correct 49 ms 12224 KB Output is correct
56 Correct 77 ms 12180 KB Output is correct
57 Correct 69 ms 12148 KB Output is correct
58 Correct 73 ms 12156 KB Output is correct
59 Correct 71 ms 12136 KB Output is correct
60 Correct 76 ms 12180 KB Output is correct
61 Correct 100 ms 12208 KB Output is correct
62 Correct 92 ms 12208 KB Output is correct
63 Correct 84 ms 12144 KB Output is correct
64 Correct 83 ms 12124 KB Output is correct
65 Correct 86 ms 12200 KB Output is correct
66 Correct 81 ms 12108 KB Output is correct
67 Correct 70 ms 12200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 360 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 356 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 219 ms 33928 KB Output is correct
14 Correct 265 ms 38896 KB Output is correct
15 Correct 349 ms 42572 KB Output is correct
16 Correct 365 ms 45896 KB Output is correct
17 Correct 446 ms 49484 KB Output is correct
18 Correct 436 ms 49580 KB Output is correct
19 Correct 422 ms 49580 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 356 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 488 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 1 ms 468 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 468 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 1 ms 356 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 1 ms 724 KB Output is correct
58 Correct 2 ms 1364 KB Output is correct
59 Correct 3 ms 1364 KB Output is correct
60 Correct 3 ms 1380 KB Output is correct
61 Correct 3 ms 1492 KB Output is correct
62 Correct 3 ms 1364 KB Output is correct
63 Correct 3 ms 1364 KB Output is correct
64 Correct 4 ms 1364 KB Output is correct
65 Correct 3 ms 1364 KB Output is correct
66 Correct 4 ms 1492 KB Output is correct
67 Correct 3 ms 1492 KB Output is correct
68 Correct 4 ms 1492 KB Output is correct
69 Correct 4 ms 1380 KB Output is correct
70 Correct 3 ms 1492 KB Output is correct
71 Correct 16 ms 3660 KB Output is correct
72 Correct 49 ms 12224 KB Output is correct
73 Correct 77 ms 12180 KB Output is correct
74 Correct 69 ms 12148 KB Output is correct
75 Correct 73 ms 12156 KB Output is correct
76 Correct 71 ms 12136 KB Output is correct
77 Correct 76 ms 12180 KB Output is correct
78 Correct 100 ms 12208 KB Output is correct
79 Correct 92 ms 12208 KB Output is correct
80 Correct 84 ms 12144 KB Output is correct
81 Correct 83 ms 12124 KB Output is correct
82 Correct 86 ms 12200 KB Output is correct
83 Correct 81 ms 12108 KB Output is correct
84 Correct 70 ms 12200 KB Output is correct
85 Correct 205 ms 26328 KB Output is correct
86 Correct 549 ms 49496 KB Output is correct
87 Incorrect 540 ms 49740 KB Output isn't correct
88 Halted 0 ms 0 KB -