#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize "Ofast"
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef complex<double> cmplx;
template <typename T> using minPq = priority_queue<T, vector<T>, greater<T>>;
#define boost() cin.sync_with_stdio(0); cin.tie(0)
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) (v).begin(), (v).end()
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound((v).begin(), (v).end(), x) - (v).begin())
#define ubv(v, x) (upper_bound((v).begin(), (v).end(), x) - (v).begin())
template <typename T> inline void UNIQUE(vector<T> &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cout << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cout << #arr; FR(_i, n) cout << ' ' << arr[_i]; cout << endl;}
const ll MOD = 1e16 + 61;
const int X = 48394;
const int MN = 2505;
int n;
string str;
int aCost, bCost, cCost;
ll hashes[MN][MN];
ll dp[MN][MN];
int len;
vector<pll> vec;
int arr[MN];
void proc(int l, int r){
ms(arr, INF);
ll minCost = 0;
int prv = 0;
FOR(i, l, r){
int a = vec[i].sn;
FOR(j, prv, a+1) arr[j] = a;
prv = a+1;
minCost = max(minCost, dp[a][a+len]);
}
minCost = (ll) aCost*len - minCost + bCost;
FOR(i, l, r){
int a = vec[i].sn;
int pos = a;
int cur = 0;
while(arr[pos] != INF){
pos = arr[pos] + len;
cur++;
assert(pos <= n);
dp[a][pos] = max(dp[a][pos], (ll) cur*len*aCost - minCost - (ll) cur*cCost);
}
}
}
int main(){
boost();
cin >> n >> str >> aCost >> bCost >> cCost;
for(char &ch : str) ch -= 'a';
FR(i, n) FOR(j, i, n) hashes[i][j+1] = (hashes[i][j]*X + str[j]) % MOD;
for(len = 1; len <= n; len++){
int num = n-len+1;
vec.clear();
FR(i, num) vec.pb({hashes[i][i+len], i});
sort(ALL(vec));
int l = 0;
FOR(r, 1, num+1){
if(r == num || vec[r].fs != vec[r-1].fs){
proc(l, r);
l = r;
}
}
FR(i, num){
if(i) dp[i-1][i+len] = max(dp[i-1][i+len], dp[i][i+len]);
if(i < num-1) dp[i][i+len+1] = max(dp[i][i+len+1], dp[i][i+len]);
}
}
ll ans = (ll) n*aCost - dp[0][n];
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
194 ms |
41184 KB |
Output is correct |
4 |
Correct |
207 ms |
46880 KB |
Output is correct |
5 |
Correct |
256 ms |
53344 KB |
Output is correct |
6 |
Correct |
283 ms |
59972 KB |
Output is correct |
7 |
Correct |
343 ms |
67436 KB |
Output is correct |
8 |
Correct |
343 ms |
67444 KB |
Output is correct |
9 |
Correct |
332 ms |
67436 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 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 |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 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 |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
980 KB |
Output is correct |
41 |
Correct |
3 ms |
2272 KB |
Output is correct |
42 |
Correct |
8 ms |
2260 KB |
Output is correct |
43 |
Correct |
8 ms |
2260 KB |
Output is correct |
44 |
Correct |
7 ms |
2260 KB |
Output is correct |
45 |
Correct |
7 ms |
2264 KB |
Output is correct |
46 |
Correct |
8 ms |
2256 KB |
Output is correct |
47 |
Correct |
4 ms |
2260 KB |
Output is correct |
48 |
Correct |
6 ms |
2260 KB |
Output is correct |
49 |
Correct |
7 ms |
2260 KB |
Output is correct |
50 |
Correct |
6 ms |
2260 KB |
Output is correct |
51 |
Correct |
6 ms |
2260 KB |
Output is correct |
52 |
Correct |
5 ms |
2260 KB |
Output is correct |
53 |
Correct |
7 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 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 |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
980 KB |
Output is correct |
41 |
Correct |
3 ms |
2272 KB |
Output is correct |
42 |
Correct |
8 ms |
2260 KB |
Output is correct |
43 |
Correct |
8 ms |
2260 KB |
Output is correct |
44 |
Correct |
7 ms |
2260 KB |
Output is correct |
45 |
Correct |
7 ms |
2264 KB |
Output is correct |
46 |
Correct |
8 ms |
2256 KB |
Output is correct |
47 |
Correct |
4 ms |
2260 KB |
Output is correct |
48 |
Correct |
6 ms |
2260 KB |
Output is correct |
49 |
Correct |
7 ms |
2260 KB |
Output is correct |
50 |
Correct |
6 ms |
2260 KB |
Output is correct |
51 |
Correct |
6 ms |
2260 KB |
Output is correct |
52 |
Correct |
5 ms |
2260 KB |
Output is correct |
53 |
Correct |
7 ms |
2260 KB |
Output is correct |
54 |
Correct |
36 ms |
5460 KB |
Output is correct |
55 |
Correct |
43 ms |
16212 KB |
Output is correct |
56 |
Correct |
194 ms |
16240 KB |
Output is correct |
57 |
Correct |
187 ms |
16240 KB |
Output is correct |
58 |
Correct |
184 ms |
16212 KB |
Output is correct |
59 |
Correct |
193 ms |
16192 KB |
Output is correct |
60 |
Correct |
184 ms |
16212 KB |
Output is correct |
61 |
Correct |
117 ms |
16236 KB |
Output is correct |
62 |
Correct |
50 ms |
16212 KB |
Output is correct |
63 |
Correct |
158 ms |
16144 KB |
Output is correct |
64 |
Correct |
159 ms |
16340 KB |
Output is correct |
65 |
Correct |
127 ms |
16240 KB |
Output is correct |
66 |
Correct |
127 ms |
16212 KB |
Output is correct |
67 |
Correct |
182 ms |
16204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
194 ms |
41184 KB |
Output is correct |
14 |
Correct |
207 ms |
46880 KB |
Output is correct |
15 |
Correct |
256 ms |
53344 KB |
Output is correct |
16 |
Correct |
283 ms |
59972 KB |
Output is correct |
17 |
Correct |
343 ms |
67436 KB |
Output is correct |
18 |
Correct |
343 ms |
67444 KB |
Output is correct |
19 |
Correct |
332 ms |
67436 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 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 |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 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 |
0 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 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 |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
980 KB |
Output is correct |
58 |
Correct |
3 ms |
2272 KB |
Output is correct |
59 |
Correct |
8 ms |
2260 KB |
Output is correct |
60 |
Correct |
8 ms |
2260 KB |
Output is correct |
61 |
Correct |
7 ms |
2260 KB |
Output is correct |
62 |
Correct |
7 ms |
2264 KB |
Output is correct |
63 |
Correct |
8 ms |
2256 KB |
Output is correct |
64 |
Correct |
4 ms |
2260 KB |
Output is correct |
65 |
Correct |
6 ms |
2260 KB |
Output is correct |
66 |
Correct |
7 ms |
2260 KB |
Output is correct |
67 |
Correct |
6 ms |
2260 KB |
Output is correct |
68 |
Correct |
6 ms |
2260 KB |
Output is correct |
69 |
Correct |
5 ms |
2260 KB |
Output is correct |
70 |
Correct |
7 ms |
2260 KB |
Output is correct |
71 |
Correct |
36 ms |
5460 KB |
Output is correct |
72 |
Correct |
43 ms |
16212 KB |
Output is correct |
73 |
Correct |
194 ms |
16240 KB |
Output is correct |
74 |
Correct |
187 ms |
16240 KB |
Output is correct |
75 |
Correct |
184 ms |
16212 KB |
Output is correct |
76 |
Correct |
193 ms |
16192 KB |
Output is correct |
77 |
Correct |
184 ms |
16212 KB |
Output is correct |
78 |
Correct |
117 ms |
16236 KB |
Output is correct |
79 |
Correct |
50 ms |
16212 KB |
Output is correct |
80 |
Correct |
158 ms |
16144 KB |
Output is correct |
81 |
Correct |
159 ms |
16340 KB |
Output is correct |
82 |
Correct |
127 ms |
16240 KB |
Output is correct |
83 |
Correct |
127 ms |
16212 KB |
Output is correct |
84 |
Correct |
182 ms |
16204 KB |
Output is correct |
85 |
Correct |
504 ms |
32704 KB |
Output is correct |
86 |
Correct |
1325 ms |
67524 KB |
Output is correct |
87 |
Correct |
1361 ms |
67552 KB |
Output is correct |
88 |
Correct |
1340 ms |
67444 KB |
Output is correct |
89 |
Correct |
1361 ms |
67552 KB |
Output is correct |
90 |
Correct |
1330 ms |
67480 KB |
Output is correct |
91 |
Correct |
482 ms |
67436 KB |
Output is correct |
92 |
Correct |
442 ms |
67440 KB |
Output is correct |
93 |
Correct |
1128 ms |
67476 KB |
Output is correct |
94 |
Correct |
1219 ms |
67684 KB |
Output is correct |
95 |
Correct |
1006 ms |
67532 KB |
Output is correct |
96 |
Correct |
1002 ms |
67444 KB |
Output is correct |
97 |
Correct |
1332 ms |
67568 KB |
Output is correct |