#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 2525; // PLZ CHK!
template <ll P, ll M> struct Hash {
vector<ll> H,B;
void build(const string &S) {
H.resize(sz(S)+1), B.resize(sz(S)+1);
B[0]=1;
for (int i=1; i<=sz(S); i++) H[i]=(H[i-1]*P+S[i-1])%M;
for (int i=1; i<=sz(S); i++) B[i]=B[i-1]*P%M;
}
ll get(int s, int e) {
return (H[e]-H[s-1]*B[e-s+1]%M+M)%M;
}
};
Hash<1299709, 1'000'000'007> H1;
Hash<1301021, 1'000'000'009> H2;
int N;
ll A,B,C,D[MAX][MAX],I[MAX][MAX];
string S;
int main() {
fio();
cin>>N>>S>>A>>B>>C;
H1.build(S), H2.build(S);
S=' '+S;
map<pll,vector<pii>> idx;
for (int i=1; i<=N; i++) {
for (int j=i; j<=N; j++) {
idx[{H1.get(i,j),H2.get(i,j)}].pb({i,j});
}
}
fill_n(&I[0][0],MAX*MAX,N+10);
for (auto &[t,v]:idx) {
queue<int> q;
for (int i=0; i<sz(v); i++) {
auto [s,e]=v[i];
while (!q.empty() && v[q.front()].second<s) {
auto [x,y]=v[q.front()];
I[x][y]=v[i].first;
q.pop();
}
q.push(i);
}
}
fill_n(&D[0][0],MAX*MAX,LINF);
for (int i=1; i<=N; i++) D[i][i]=A+B;
for (int i=1; i<=N; i++) {
for (int l=1,r=l+i-1; r<=N; l++,r++) {
if (l>1) D[l-1][r]=min(D[l-1][r], A+D[l][r]);
if (r<N) D[l][r+1]=min(D[l][r+1], A+D[l][r]);
int cur=I[l][r], cnt=2;
while (cur+i-1<=N) {
D[l][cur+i-1]=min(D[l][cur+i-1], D[l][r]+C*cnt+A*(cur+i-l-cnt*i)+B);
cnt++,cur=I[cur][cur+i-1];
}
}
}
cout<<D[1][N]-B;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
100176 KB |
Output is correct |
2 |
Correct |
16 ms |
100176 KB |
Output is correct |
3 |
Correct |
16 ms |
100176 KB |
Output is correct |
4 |
Correct |
15 ms |
100140 KB |
Output is correct |
5 |
Correct |
15 ms |
100340 KB |
Output is correct |
6 |
Correct |
15 ms |
100176 KB |
Output is correct |
7 |
Correct |
16 ms |
100176 KB |
Output is correct |
8 |
Correct |
16 ms |
100176 KB |
Output is correct |
9 |
Correct |
14 ms |
100176 KB |
Output is correct |
10 |
Correct |
15 ms |
100176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
100176 KB |
Output is correct |
2 |
Correct |
14 ms |
100176 KB |
Output is correct |
3 |
Correct |
160 ms |
118760 KB |
Output is correct |
4 |
Correct |
190 ms |
120904 KB |
Output is correct |
5 |
Correct |
233 ms |
124064 KB |
Output is correct |
6 |
Correct |
341 ms |
129424 KB |
Output is correct |
7 |
Correct |
367 ms |
135252 KB |
Output is correct |
8 |
Correct |
366 ms |
135188 KB |
Output is correct |
9 |
Correct |
370 ms |
135240 KB |
Output is correct |
10 |
Correct |
15 ms |
100176 KB |
Output is correct |
11 |
Correct |
14 ms |
100176 KB |
Output is correct |
12 |
Correct |
13 ms |
100216 KB |
Output is correct |
13 |
Correct |
14 ms |
100188 KB |
Output is correct |
14 |
Correct |
14 ms |
100176 KB |
Output is correct |
15 |
Correct |
14 ms |
100176 KB |
Output is correct |
16 |
Correct |
14 ms |
100260 KB |
Output is correct |
17 |
Correct |
14 ms |
100176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
100176 KB |
Output is correct |
2 |
Correct |
16 ms |
100176 KB |
Output is correct |
3 |
Correct |
16 ms |
100176 KB |
Output is correct |
4 |
Correct |
15 ms |
100140 KB |
Output is correct |
5 |
Correct |
15 ms |
100340 KB |
Output is correct |
6 |
Correct |
15 ms |
100176 KB |
Output is correct |
7 |
Correct |
16 ms |
100176 KB |
Output is correct |
8 |
Correct |
16 ms |
100176 KB |
Output is correct |
9 |
Correct |
14 ms |
100176 KB |
Output is correct |
10 |
Correct |
15 ms |
100176 KB |
Output is correct |
11 |
Correct |
14 ms |
100124 KB |
Output is correct |
12 |
Correct |
14 ms |
100268 KB |
Output is correct |
13 |
Correct |
14 ms |
100136 KB |
Output is correct |
14 |
Correct |
14 ms |
100176 KB |
Output is correct |
15 |
Correct |
15 ms |
100188 KB |
Output is correct |
16 |
Correct |
14 ms |
100176 KB |
Output is correct |
17 |
Correct |
14 ms |
100036 KB |
Output is correct |
18 |
Correct |
16 ms |
100176 KB |
Output is correct |
19 |
Correct |
15 ms |
100176 KB |
Output is correct |
20 |
Correct |
15 ms |
100176 KB |
Output is correct |
21 |
Correct |
15 ms |
100260 KB |
Output is correct |
22 |
Correct |
15 ms |
100344 KB |
Output is correct |
23 |
Correct |
19 ms |
100176 KB |
Output is correct |
24 |
Correct |
15 ms |
100176 KB |
Output is correct |
25 |
Correct |
14 ms |
100176 KB |
Output is correct |
26 |
Correct |
14 ms |
100176 KB |
Output is correct |
27 |
Correct |
14 ms |
100176 KB |
Output is correct |
28 |
Correct |
14 ms |
100308 KB |
Output is correct |
29 |
Correct |
15 ms |
100176 KB |
Output is correct |
30 |
Correct |
15 ms |
100176 KB |
Output is correct |
31 |
Correct |
14 ms |
100176 KB |
Output is correct |
32 |
Correct |
14 ms |
100344 KB |
Output is correct |
33 |
Correct |
15 ms |
100176 KB |
Output is correct |
34 |
Correct |
15 ms |
100176 KB |
Output is correct |
35 |
Correct |
15 ms |
100176 KB |
Output is correct |
36 |
Correct |
16 ms |
100176 KB |
Output is correct |
37 |
Correct |
14 ms |
100344 KB |
Output is correct |
38 |
Correct |
14 ms |
100228 KB |
Output is correct |
39 |
Correct |
15 ms |
100176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
100176 KB |
Output is correct |
2 |
Correct |
16 ms |
100176 KB |
Output is correct |
3 |
Correct |
16 ms |
100176 KB |
Output is correct |
4 |
Correct |
15 ms |
100140 KB |
Output is correct |
5 |
Correct |
15 ms |
100340 KB |
Output is correct |
6 |
Correct |
15 ms |
100176 KB |
Output is correct |
7 |
Correct |
16 ms |
100176 KB |
Output is correct |
8 |
Correct |
16 ms |
100176 KB |
Output is correct |
9 |
Correct |
14 ms |
100176 KB |
Output is correct |
10 |
Correct |
15 ms |
100176 KB |
Output is correct |
11 |
Correct |
14 ms |
100124 KB |
Output is correct |
12 |
Correct |
14 ms |
100268 KB |
Output is correct |
13 |
Correct |
14 ms |
100136 KB |
Output is correct |
14 |
Correct |
14 ms |
100176 KB |
Output is correct |
15 |
Correct |
15 ms |
100188 KB |
Output is correct |
16 |
Correct |
14 ms |
100176 KB |
Output is correct |
17 |
Correct |
14 ms |
100036 KB |
Output is correct |
18 |
Correct |
16 ms |
100176 KB |
Output is correct |
19 |
Correct |
15 ms |
100176 KB |
Output is correct |
20 |
Correct |
15 ms |
100176 KB |
Output is correct |
21 |
Correct |
15 ms |
100260 KB |
Output is correct |
22 |
Correct |
15 ms |
100344 KB |
Output is correct |
23 |
Correct |
19 ms |
100176 KB |
Output is correct |
24 |
Correct |
15 ms |
100176 KB |
Output is correct |
25 |
Correct |
14 ms |
100176 KB |
Output is correct |
26 |
Correct |
14 ms |
100176 KB |
Output is correct |
27 |
Correct |
14 ms |
100176 KB |
Output is correct |
28 |
Correct |
14 ms |
100308 KB |
Output is correct |
29 |
Correct |
15 ms |
100176 KB |
Output is correct |
30 |
Correct |
15 ms |
100176 KB |
Output is correct |
31 |
Correct |
14 ms |
100176 KB |
Output is correct |
32 |
Correct |
14 ms |
100344 KB |
Output is correct |
33 |
Correct |
15 ms |
100176 KB |
Output is correct |
34 |
Correct |
15 ms |
100176 KB |
Output is correct |
35 |
Correct |
15 ms |
100176 KB |
Output is correct |
36 |
Correct |
16 ms |
100176 KB |
Output is correct |
37 |
Correct |
14 ms |
100344 KB |
Output is correct |
38 |
Correct |
14 ms |
100228 KB |
Output is correct |
39 |
Correct |
15 ms |
100176 KB |
Output is correct |
40 |
Correct |
22 ms |
100432 KB |
Output is correct |
41 |
Correct |
19 ms |
100432 KB |
Output is correct |
42 |
Correct |
28 ms |
102168 KB |
Output is correct |
43 |
Correct |
23 ms |
102216 KB |
Output is correct |
44 |
Correct |
26 ms |
102340 KB |
Output is correct |
45 |
Correct |
29 ms |
102224 KB |
Output is correct |
46 |
Correct |
28 ms |
102244 KB |
Output is correct |
47 |
Correct |
20 ms |
100944 KB |
Output is correct |
48 |
Correct |
21 ms |
101456 KB |
Output is correct |
49 |
Correct |
23 ms |
101712 KB |
Output is correct |
50 |
Correct |
24 ms |
101712 KB |
Output is correct |
51 |
Correct |
22 ms |
101456 KB |
Output is correct |
52 |
Correct |
21 ms |
101456 KB |
Output is correct |
53 |
Correct |
23 ms |
102224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
100176 KB |
Output is correct |
2 |
Correct |
16 ms |
100176 KB |
Output is correct |
3 |
Correct |
16 ms |
100176 KB |
Output is correct |
4 |
Correct |
15 ms |
100140 KB |
Output is correct |
5 |
Correct |
15 ms |
100340 KB |
Output is correct |
6 |
Correct |
15 ms |
100176 KB |
Output is correct |
7 |
Correct |
16 ms |
100176 KB |
Output is correct |
8 |
Correct |
16 ms |
100176 KB |
Output is correct |
9 |
Correct |
14 ms |
100176 KB |
Output is correct |
10 |
Correct |
15 ms |
100176 KB |
Output is correct |
11 |
Correct |
14 ms |
100124 KB |
Output is correct |
12 |
Correct |
14 ms |
100268 KB |
Output is correct |
13 |
Correct |
14 ms |
100136 KB |
Output is correct |
14 |
Correct |
14 ms |
100176 KB |
Output is correct |
15 |
Correct |
15 ms |
100188 KB |
Output is correct |
16 |
Correct |
14 ms |
100176 KB |
Output is correct |
17 |
Correct |
14 ms |
100036 KB |
Output is correct |
18 |
Correct |
16 ms |
100176 KB |
Output is correct |
19 |
Correct |
15 ms |
100176 KB |
Output is correct |
20 |
Correct |
15 ms |
100176 KB |
Output is correct |
21 |
Correct |
15 ms |
100260 KB |
Output is correct |
22 |
Correct |
15 ms |
100344 KB |
Output is correct |
23 |
Correct |
19 ms |
100176 KB |
Output is correct |
24 |
Correct |
15 ms |
100176 KB |
Output is correct |
25 |
Correct |
14 ms |
100176 KB |
Output is correct |
26 |
Correct |
14 ms |
100176 KB |
Output is correct |
27 |
Correct |
14 ms |
100176 KB |
Output is correct |
28 |
Correct |
14 ms |
100308 KB |
Output is correct |
29 |
Correct |
15 ms |
100176 KB |
Output is correct |
30 |
Correct |
15 ms |
100176 KB |
Output is correct |
31 |
Correct |
14 ms |
100176 KB |
Output is correct |
32 |
Correct |
14 ms |
100344 KB |
Output is correct |
33 |
Correct |
15 ms |
100176 KB |
Output is correct |
34 |
Correct |
15 ms |
100176 KB |
Output is correct |
35 |
Correct |
15 ms |
100176 KB |
Output is correct |
36 |
Correct |
16 ms |
100176 KB |
Output is correct |
37 |
Correct |
14 ms |
100344 KB |
Output is correct |
38 |
Correct |
14 ms |
100228 KB |
Output is correct |
39 |
Correct |
15 ms |
100176 KB |
Output is correct |
40 |
Correct |
22 ms |
100432 KB |
Output is correct |
41 |
Correct |
19 ms |
100432 KB |
Output is correct |
42 |
Correct |
28 ms |
102168 KB |
Output is correct |
43 |
Correct |
23 ms |
102216 KB |
Output is correct |
44 |
Correct |
26 ms |
102340 KB |
Output is correct |
45 |
Correct |
29 ms |
102224 KB |
Output is correct |
46 |
Correct |
28 ms |
102244 KB |
Output is correct |
47 |
Correct |
20 ms |
100944 KB |
Output is correct |
48 |
Correct |
21 ms |
101456 KB |
Output is correct |
49 |
Correct |
23 ms |
101712 KB |
Output is correct |
50 |
Correct |
24 ms |
101712 KB |
Output is correct |
51 |
Correct |
22 ms |
101456 KB |
Output is correct |
52 |
Correct |
21 ms |
101456 KB |
Output is correct |
53 |
Correct |
23 ms |
102224 KB |
Output is correct |
54 |
Correct |
75 ms |
110664 KB |
Output is correct |
55 |
Correct |
46 ms |
105552 KB |
Output is correct |
56 |
Correct |
501 ms |
154240 KB |
Output is correct |
57 |
Correct |
552 ms |
154388 KB |
Output is correct |
58 |
Correct |
590 ms |
154768 KB |
Output is correct |
59 |
Correct |
625 ms |
154696 KB |
Output is correct |
60 |
Correct |
625 ms |
154952 KB |
Output is correct |
61 |
Correct |
327 ms |
127968 KB |
Output is correct |
62 |
Correct |
68 ms |
106436 KB |
Output is correct |
63 |
Correct |
511 ms |
141672 KB |
Output is correct |
64 |
Correct |
463 ms |
142444 KB |
Output is correct |
65 |
Correct |
480 ms |
129228 KB |
Output is correct |
66 |
Correct |
339 ms |
129200 KB |
Output is correct |
67 |
Correct |
484 ms |
154896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
100176 KB |
Output is correct |
2 |
Correct |
16 ms |
100176 KB |
Output is correct |
3 |
Correct |
16 ms |
100176 KB |
Output is correct |
4 |
Correct |
15 ms |
100140 KB |
Output is correct |
5 |
Correct |
15 ms |
100340 KB |
Output is correct |
6 |
Correct |
15 ms |
100176 KB |
Output is correct |
7 |
Correct |
16 ms |
100176 KB |
Output is correct |
8 |
Correct |
16 ms |
100176 KB |
Output is correct |
9 |
Correct |
14 ms |
100176 KB |
Output is correct |
10 |
Correct |
15 ms |
100176 KB |
Output is correct |
11 |
Correct |
15 ms |
100176 KB |
Output is correct |
12 |
Correct |
14 ms |
100176 KB |
Output is correct |
13 |
Correct |
160 ms |
118760 KB |
Output is correct |
14 |
Correct |
190 ms |
120904 KB |
Output is correct |
15 |
Correct |
233 ms |
124064 KB |
Output is correct |
16 |
Correct |
341 ms |
129424 KB |
Output is correct |
17 |
Correct |
367 ms |
135252 KB |
Output is correct |
18 |
Correct |
366 ms |
135188 KB |
Output is correct |
19 |
Correct |
370 ms |
135240 KB |
Output is correct |
20 |
Correct |
15 ms |
100176 KB |
Output is correct |
21 |
Correct |
14 ms |
100176 KB |
Output is correct |
22 |
Correct |
13 ms |
100216 KB |
Output is correct |
23 |
Correct |
14 ms |
100188 KB |
Output is correct |
24 |
Correct |
14 ms |
100176 KB |
Output is correct |
25 |
Correct |
14 ms |
100176 KB |
Output is correct |
26 |
Correct |
14 ms |
100260 KB |
Output is correct |
27 |
Correct |
14 ms |
100176 KB |
Output is correct |
28 |
Correct |
14 ms |
100124 KB |
Output is correct |
29 |
Correct |
14 ms |
100268 KB |
Output is correct |
30 |
Correct |
14 ms |
100136 KB |
Output is correct |
31 |
Correct |
14 ms |
100176 KB |
Output is correct |
32 |
Correct |
15 ms |
100188 KB |
Output is correct |
33 |
Correct |
14 ms |
100176 KB |
Output is correct |
34 |
Correct |
14 ms |
100036 KB |
Output is correct |
35 |
Correct |
16 ms |
100176 KB |
Output is correct |
36 |
Correct |
15 ms |
100176 KB |
Output is correct |
37 |
Correct |
15 ms |
100176 KB |
Output is correct |
38 |
Correct |
15 ms |
100260 KB |
Output is correct |
39 |
Correct |
15 ms |
100344 KB |
Output is correct |
40 |
Correct |
19 ms |
100176 KB |
Output is correct |
41 |
Correct |
15 ms |
100176 KB |
Output is correct |
42 |
Correct |
14 ms |
100176 KB |
Output is correct |
43 |
Correct |
14 ms |
100176 KB |
Output is correct |
44 |
Correct |
14 ms |
100176 KB |
Output is correct |
45 |
Correct |
14 ms |
100308 KB |
Output is correct |
46 |
Correct |
15 ms |
100176 KB |
Output is correct |
47 |
Correct |
15 ms |
100176 KB |
Output is correct |
48 |
Correct |
14 ms |
100176 KB |
Output is correct |
49 |
Correct |
14 ms |
100344 KB |
Output is correct |
50 |
Correct |
15 ms |
100176 KB |
Output is correct |
51 |
Correct |
15 ms |
100176 KB |
Output is correct |
52 |
Correct |
15 ms |
100176 KB |
Output is correct |
53 |
Correct |
16 ms |
100176 KB |
Output is correct |
54 |
Correct |
14 ms |
100344 KB |
Output is correct |
55 |
Correct |
14 ms |
100228 KB |
Output is correct |
56 |
Correct |
15 ms |
100176 KB |
Output is correct |
57 |
Correct |
22 ms |
100432 KB |
Output is correct |
58 |
Correct |
19 ms |
100432 KB |
Output is correct |
59 |
Correct |
28 ms |
102168 KB |
Output is correct |
60 |
Correct |
23 ms |
102216 KB |
Output is correct |
61 |
Correct |
26 ms |
102340 KB |
Output is correct |
62 |
Correct |
29 ms |
102224 KB |
Output is correct |
63 |
Correct |
28 ms |
102244 KB |
Output is correct |
64 |
Correct |
20 ms |
100944 KB |
Output is correct |
65 |
Correct |
21 ms |
101456 KB |
Output is correct |
66 |
Correct |
23 ms |
101712 KB |
Output is correct |
67 |
Correct |
24 ms |
101712 KB |
Output is correct |
68 |
Correct |
22 ms |
101456 KB |
Output is correct |
69 |
Correct |
21 ms |
101456 KB |
Output is correct |
70 |
Correct |
23 ms |
102224 KB |
Output is correct |
71 |
Correct |
75 ms |
110664 KB |
Output is correct |
72 |
Correct |
46 ms |
105552 KB |
Output is correct |
73 |
Correct |
501 ms |
154240 KB |
Output is correct |
74 |
Correct |
552 ms |
154388 KB |
Output is correct |
75 |
Correct |
590 ms |
154768 KB |
Output is correct |
76 |
Correct |
625 ms |
154696 KB |
Output is correct |
77 |
Correct |
625 ms |
154952 KB |
Output is correct |
78 |
Correct |
327 ms |
127968 KB |
Output is correct |
79 |
Correct |
68 ms |
106436 KB |
Output is correct |
80 |
Correct |
511 ms |
141672 KB |
Output is correct |
81 |
Correct |
463 ms |
142444 KB |
Output is correct |
82 |
Correct |
480 ms |
129228 KB |
Output is correct |
83 |
Correct |
339 ms |
129200 KB |
Output is correct |
84 |
Correct |
484 ms |
154896 KB |
Output is correct |
85 |
Correct |
1724 ms |
235632 KB |
Output is correct |
86 |
Execution timed out |
3056 ms |
257180 KB |
Time limit exceeded |
87 |
Halted |
0 ms |
0 KB |
- |