#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7, mod2 = 998422353;
#define ml(x,y) ((x*y)%mod)
ll b1 = 53, b2 = 47, p1[3003], p2[3003];
ll inv[3003];
ll a, b, c;
map<ll, ll> dp;
inline ll fsp(ll x, ll b) {
if (b == 0) return 1;
if (b == 1) return x;
ll g = ml(x, x);
g = fsp(g, b / 2);
if (b % 2 == 0) return g;
return (g * x) % mod;
}
inline ll small(vector<ll> k) {
if (k.size() == 1) return a;
if (k.size() == 2) {
ll fr = k[0], sc = (k[1] - k[0] + mod) % mod;
sc = (sc * inv[1]) % mod;
if (fr == sc) return min(2 * a, a + b + 2 * c);
return 2 * a;
}
ll fr = k[0], sc = (k[1] - k[0] + mod) % mod, th = (k[2] - k[1] + mod) % mod;
sc = (sc * inv[1]) % mod;
th = (th * inv[2]) % mod;
if (sc != fr && fr != th && sc != th) return 3 * a;
if (sc == fr && fr == th) return min(3 * a, a + b + 3 * c);
return min(3 * a, 2 * a + b + 2 * c);
}
inline ll getmin(vector<ll> k) {
ll th = k[k.size() - 1];
if (dp.find(th) != dp.end()) return dp[th];
if (k.size() <= 3) return small(k);
ll dc = 0, tot = (ll)k.size() * a;
for (ll ln = 1; ln <= k.size() / 2; ++ln) {
map<ll, ll> m, d; map<ll, pair<ll, ll>> st;
for (ll i = 0; i + ln <= k.size(); ++i) {
ll j = i + ln - 1;
ll hs = (k[j] - (i > 0 ? k[i - 1] : 0) + mod) % mod;
hs = (hs * inv[i]) % mod;
if (m.find(hs) != m.end()) {
if (i < d[hs]) continue;
}
else st[hs] = { i,j };
d[hs] = i + ln;
++m[hs];
}
for (map<ll, ll>::iterator it = m.begin(); it != m.end(); ++it) {
vector<ll> nh; pair<ll, ll> se = st[it->first];
ll bg = se.first;
for (ll j = se.first; j <= se.second; ++j) {
ll hvl = (k[j] - (bg == 0 ? 0 : k[bg - 1]) + mod) % mod;
hvl = (hvl * inv[bg]) % mod;
nh.push_back(hvl);
}
ll prp = getmin(nh);
ll tcost = it->second * ln * a - prp - it->second * c - b;
if (tcost <= 0) continue;
dc = max(dc, tcost);
}
}
dp[th] = tot - dc;
return tot - dc;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n; cin >> n;
string s; cin >> s;
cin >> a >> b >> c;
p1[0] = p2[0] = 1;
p1[1] = b1; p2[1] = b2;
for (ll i = 2; i <= s.size() + 3; ++i) {
p1[i] = (p1[i - 1] * b1) % mod;
p2[i] = (p2[i - 1] * b2) % mod2;
}
inv[0] = 1;
for (ll i = 1; i <= s.size() + 3; ++i) {
inv[i] = fsp(p1[i], mod - 2);
}
vector<ll> vhash; ll lst = 0;
for (ll i = 0; i < s.size(); ++i) {
ll hv = ((s[i] - 95) * p1[i]) % mod;
hv = (hv + lst) % mod;
vhash.push_back(hv);
lst = hv;
}
cout << getmin(vhash);
}
Compilation message
copypaste3.cpp: In function 'll getmin(std::vector<long long int>)':
copypaste3.cpp:43:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (ll ln = 1; ln <= k.size() / 2; ++ln) {
| ~~~^~~~~~~~~~~~~~~
copypaste3.cpp:45:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (ll i = 0; i + ln <= k.size(); ++i) {
| ~~~~~~~^~~~~~~~~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:81:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (ll i = 2; i <= s.size() + 3; ++i) {
| ~~^~~~~~~~~~~~~~~
copypaste3.cpp:86:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (ll i = 1; i <= s.size() + 3; ++i) {
| ~~^~~~~~~~~~~~~~~
copypaste3.cpp:90:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (ll i = 0; i < s.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
663 ms |
600 KB |
Output is correct |
4 |
Correct |
882 ms |
816 KB |
Output is correct |
5 |
Correct |
1043 ms |
1104 KB |
Output is correct |
6 |
Correct |
1296 ms |
888 KB |
Output is correct |
7 |
Correct |
1645 ms |
868 KB |
Output is correct |
8 |
Correct |
1637 ms |
884 KB |
Output is correct |
9 |
Correct |
1646 ms |
892 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
464 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
484 KB |
Output is correct |
33 |
Correct |
3 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
464 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
484 KB |
Output is correct |
33 |
Correct |
3 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
464 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
162 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
344 KB |
Output is correct |
42 |
Execution timed out |
3082 ms |
1304 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
484 KB |
Output is correct |
33 |
Correct |
3 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
464 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
162 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
344 KB |
Output is correct |
42 |
Execution timed out |
3082 ms |
1304 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
663 ms |
600 KB |
Output is correct |
14 |
Correct |
882 ms |
816 KB |
Output is correct |
15 |
Correct |
1043 ms |
1104 KB |
Output is correct |
16 |
Correct |
1296 ms |
888 KB |
Output is correct |
17 |
Correct |
1645 ms |
868 KB |
Output is correct |
18 |
Correct |
1637 ms |
884 KB |
Output is correct |
19 |
Correct |
1646 ms |
892 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
464 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
2 ms |
348 KB |
Output is correct |
40 |
Correct |
3 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
348 KB |
Output is correct |
42 |
Correct |
3 ms |
348 KB |
Output is correct |
43 |
Correct |
3 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
3 ms |
348 KB |
Output is correct |
47 |
Correct |
2 ms |
348 KB |
Output is correct |
48 |
Correct |
2 ms |
344 KB |
Output is correct |
49 |
Correct |
2 ms |
484 KB |
Output is correct |
50 |
Correct |
3 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
464 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
162 ms |
596 KB |
Output is correct |
58 |
Correct |
2 ms |
344 KB |
Output is correct |
59 |
Execution timed out |
3082 ms |
1304 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |