#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,s,e) for(int i = s; i <= (int)e; ++i)
#define DEC(i,s,e) for(int i = s; i >= (int)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{" << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
#define reach cerr << "LINE: " << __LINE__ << "\n";
#else
#define reach
#define db(x)
#define db2(x,y)
#define db3(a,b,c)
#define dbv(v)
#define dbvp(v)
#define dba(a,ss,ee)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define g0(x) get<0>(x)
#define g1(x) get<1>(x)
#define g2(x) get<2>(x)
#define g3(x) get<3>(x)
typedef pair <int, int> pi;
typedef tuple<int,int,int> ti3;
typedef tuple<int,int,int,int> ti4;
int rand(int a, int b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (long long)1e18 + 500;
template <typename T> void chmax(T& a, const T b) { a=max(a,b); }
template <typename T> void chmin(T& a, const T b) { a=min(a,b); }
const int MAXN = 2505;
int H[MAXN][MAXN];
int n;
int A,B,C;
int s[MAXN];
int T[MAXN];
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,tune=native")
#pragma GCC optimize("O3,unroll-loops")
int qexp(int a, int b, int mod){
int res=1;
while(b){
if(b&1)res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
unordered_map<int,vector<pi>> V;
unordered_map<int,int> dp2;
int dpdp(int l, int r);
int imp;
int transit(int hash){
if(dp2.find(hash)!=dp2.end())return dp2[hash];
int L=V[hash][0].s-V[hash][0].f+1;
vector<int> pts;
vector<int> sts,ens;
unordered_map<int,vector<int>> vv;
for(auto seg:V[hash]){
sts.pb(seg.f-1);
pts.pb(seg.f-1);
pts.pb(seg.s);
ens.pb(seg.s);
}
sort(all(sts));
sts.resize(unique(all(sts))-sts.begin());
sort(all(ens));
ens.resize(unique(all(ens))-ens.begin());
sort(all(pts));
pts.resize(unique(all(pts))-pts.begin());
for(auto seg:V[hash]){
int l=lower_bound(all(pts),seg.f-1)-pts.begin();
int r=lower_bound(all(pts),seg.s)-pts.begin();
vv[r].pb(l);
}
int ans=oo;
FOR(l,0,sts.size()-1){
vector<int> mn;
int idx=0;
unordered_map<int,int> mp;
FOR(i,0,pts.size()-1){
if(pts[i]<sts[l])continue;
if(pts[i]==sts[l]){
mp[pts[i]]=0;
mn.pb(0);
continue;
}
++idx;
mp[pts[i]]=idx;
int val=oo;
for(auto x:vv[i]){
if(pts[x]>=sts[l]) {
chmin(val,mn[mp[pts[x]]]+C);
}
}
chmin(val,mn.back()+A*(pts[i]-pts[i-1]));
mn.pb(val);
if(pts[i]-sts[l]>=L*2)chmin(ans, val+dpdp(sts[l]+1,pts[i]));
}
}
return dp2[hash]=ans;
}
int dp1[MAXN][MAXN];
int dpdp(int l, int r){
if(~dp1[l][r]) return dp1[l][r];
if(l==1&&r==n)return 0;
int ans=oo;
if(r!=n)chmin(ans,dpdp(l,r+1)+A);
if(l!=1)chmin(ans,dpdp(l-1,r)+A);
chmin(ans,transit(H[l][r])+B);
return dp1[l][r]=ans;
}
int32_t main()
{
IAMSPEED
memset(dp1,-1,sizeof dp1);
cin >> n;
FOR(i,1,n){
char ch; cin >> ch;
s[i]=ch-'a'+1;
}
cin >> A >> B >> C;
FOR(i,1,n){
int val=qexp(27,i,MOD)*s[i];
T[i]=T[i-1]+val;
}
set<int> hashes;
FOR(i,1,n){
FOR(j,i,n){
int d=qexp(27,i-1,MOD);
H[i][j]=((T[j]-T[i-1]+MOD)%MOD)*qexp(d,MOD-2,MOD)%MOD;
V[H[i][j]].pb({i,j});
hashes.insert(H[i][j]);
}
}
int ans=oo;
FOR(i,1,n){
int res=dpdp(i,i)+A;
chmin(ans,res);
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
49364 KB |
Output is correct |
2 |
Correct |
22 ms |
49400 KB |
Output is correct |
3 |
Correct |
21 ms |
49392 KB |
Output is correct |
4 |
Correct |
21 ms |
49364 KB |
Output is correct |
5 |
Correct |
21 ms |
49400 KB |
Output is correct |
6 |
Correct |
22 ms |
49352 KB |
Output is correct |
7 |
Correct |
23 ms |
49372 KB |
Output is correct |
8 |
Correct |
21 ms |
49364 KB |
Output is correct |
9 |
Correct |
22 ms |
49356 KB |
Output is correct |
10 |
Correct |
21 ms |
49452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
49452 KB |
Output is correct |
2 |
Correct |
21 ms |
49412 KB |
Output is correct |
3 |
Execution timed out |
3080 ms |
104680 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
49364 KB |
Output is correct |
2 |
Correct |
22 ms |
49400 KB |
Output is correct |
3 |
Correct |
21 ms |
49392 KB |
Output is correct |
4 |
Correct |
21 ms |
49364 KB |
Output is correct |
5 |
Correct |
21 ms |
49400 KB |
Output is correct |
6 |
Correct |
22 ms |
49352 KB |
Output is correct |
7 |
Correct |
23 ms |
49372 KB |
Output is correct |
8 |
Correct |
21 ms |
49364 KB |
Output is correct |
9 |
Correct |
22 ms |
49356 KB |
Output is correct |
10 |
Correct |
21 ms |
49452 KB |
Output is correct |
11 |
Correct |
25 ms |
49484 KB |
Output is correct |
12 |
Correct |
29 ms |
49492 KB |
Output is correct |
13 |
Correct |
25 ms |
49492 KB |
Output is correct |
14 |
Correct |
24 ms |
49460 KB |
Output is correct |
15 |
Correct |
21 ms |
49568 KB |
Output is correct |
16 |
Correct |
21 ms |
49392 KB |
Output is correct |
17 |
Correct |
29 ms |
49356 KB |
Output is correct |
18 |
Correct |
30 ms |
49420 KB |
Output is correct |
19 |
Correct |
23 ms |
49448 KB |
Output is correct |
20 |
Correct |
25 ms |
49440 KB |
Output is correct |
21 |
Correct |
23 ms |
49576 KB |
Output is correct |
22 |
Correct |
24 ms |
49620 KB |
Output is correct |
23 |
Correct |
22 ms |
49576 KB |
Output is correct |
24 |
Correct |
24 ms |
49620 KB |
Output is correct |
25 |
Correct |
21 ms |
49616 KB |
Output is correct |
26 |
Correct |
25 ms |
49648 KB |
Output is correct |
27 |
Correct |
26 ms |
49604 KB |
Output is correct |
28 |
Correct |
26 ms |
49600 KB |
Output is correct |
29 |
Correct |
25 ms |
49572 KB |
Output is correct |
30 |
Correct |
21 ms |
49592 KB |
Output is correct |
31 |
Correct |
24 ms |
49580 KB |
Output is correct |
32 |
Correct |
22 ms |
49620 KB |
Output is correct |
33 |
Correct |
22 ms |
49564 KB |
Output is correct |
34 |
Correct |
22 ms |
49400 KB |
Output is correct |
35 |
Correct |
20 ms |
49356 KB |
Output is correct |
36 |
Correct |
21 ms |
49432 KB |
Output is correct |
37 |
Correct |
21 ms |
49444 KB |
Output is correct |
38 |
Correct |
20 ms |
49516 KB |
Output is correct |
39 |
Correct |
21 ms |
49580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
49364 KB |
Output is correct |
2 |
Correct |
22 ms |
49400 KB |
Output is correct |
3 |
Correct |
21 ms |
49392 KB |
Output is correct |
4 |
Correct |
21 ms |
49364 KB |
Output is correct |
5 |
Correct |
21 ms |
49400 KB |
Output is correct |
6 |
Correct |
22 ms |
49352 KB |
Output is correct |
7 |
Correct |
23 ms |
49372 KB |
Output is correct |
8 |
Correct |
21 ms |
49364 KB |
Output is correct |
9 |
Correct |
22 ms |
49356 KB |
Output is correct |
10 |
Correct |
21 ms |
49452 KB |
Output is correct |
11 |
Correct |
25 ms |
49484 KB |
Output is correct |
12 |
Correct |
29 ms |
49492 KB |
Output is correct |
13 |
Correct |
25 ms |
49492 KB |
Output is correct |
14 |
Correct |
24 ms |
49460 KB |
Output is correct |
15 |
Correct |
21 ms |
49568 KB |
Output is correct |
16 |
Correct |
21 ms |
49392 KB |
Output is correct |
17 |
Correct |
29 ms |
49356 KB |
Output is correct |
18 |
Correct |
30 ms |
49420 KB |
Output is correct |
19 |
Correct |
23 ms |
49448 KB |
Output is correct |
20 |
Correct |
25 ms |
49440 KB |
Output is correct |
21 |
Correct |
23 ms |
49576 KB |
Output is correct |
22 |
Correct |
24 ms |
49620 KB |
Output is correct |
23 |
Correct |
22 ms |
49576 KB |
Output is correct |
24 |
Correct |
24 ms |
49620 KB |
Output is correct |
25 |
Correct |
21 ms |
49616 KB |
Output is correct |
26 |
Correct |
25 ms |
49648 KB |
Output is correct |
27 |
Correct |
26 ms |
49604 KB |
Output is correct |
28 |
Correct |
26 ms |
49600 KB |
Output is correct |
29 |
Correct |
25 ms |
49572 KB |
Output is correct |
30 |
Correct |
21 ms |
49592 KB |
Output is correct |
31 |
Correct |
24 ms |
49580 KB |
Output is correct |
32 |
Correct |
22 ms |
49620 KB |
Output is correct |
33 |
Correct |
22 ms |
49564 KB |
Output is correct |
34 |
Correct |
22 ms |
49400 KB |
Output is correct |
35 |
Correct |
20 ms |
49356 KB |
Output is correct |
36 |
Correct |
21 ms |
49432 KB |
Output is correct |
37 |
Correct |
21 ms |
49444 KB |
Output is correct |
38 |
Correct |
20 ms |
49516 KB |
Output is correct |
39 |
Correct |
21 ms |
49580 KB |
Output is correct |
40 |
Correct |
30 ms |
50456 KB |
Output is correct |
41 |
Correct |
213 ms |
50960 KB |
Output is correct |
42 |
Correct |
54 ms |
53708 KB |
Output is correct |
43 |
Correct |
52 ms |
53716 KB |
Output is correct |
44 |
Correct |
50 ms |
53788 KB |
Output is correct |
45 |
Correct |
53 ms |
53768 KB |
Output is correct |
46 |
Correct |
48 ms |
53856 KB |
Output is correct |
47 |
Correct |
59 ms |
51600 KB |
Output is correct |
48 |
Correct |
52 ms |
52684 KB |
Output is correct |
49 |
Correct |
47 ms |
53056 KB |
Output is correct |
50 |
Correct |
63 ms |
53232 KB |
Output is correct |
51 |
Correct |
54 ms |
52624 KB |
Output is correct |
52 |
Correct |
52 ms |
52648 KB |
Output is correct |
53 |
Correct |
51 ms |
53800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
49364 KB |
Output is correct |
2 |
Correct |
22 ms |
49400 KB |
Output is correct |
3 |
Correct |
21 ms |
49392 KB |
Output is correct |
4 |
Correct |
21 ms |
49364 KB |
Output is correct |
5 |
Correct |
21 ms |
49400 KB |
Output is correct |
6 |
Correct |
22 ms |
49352 KB |
Output is correct |
7 |
Correct |
23 ms |
49372 KB |
Output is correct |
8 |
Correct |
21 ms |
49364 KB |
Output is correct |
9 |
Correct |
22 ms |
49356 KB |
Output is correct |
10 |
Correct |
21 ms |
49452 KB |
Output is correct |
11 |
Correct |
25 ms |
49484 KB |
Output is correct |
12 |
Correct |
29 ms |
49492 KB |
Output is correct |
13 |
Correct |
25 ms |
49492 KB |
Output is correct |
14 |
Correct |
24 ms |
49460 KB |
Output is correct |
15 |
Correct |
21 ms |
49568 KB |
Output is correct |
16 |
Correct |
21 ms |
49392 KB |
Output is correct |
17 |
Correct |
29 ms |
49356 KB |
Output is correct |
18 |
Correct |
30 ms |
49420 KB |
Output is correct |
19 |
Correct |
23 ms |
49448 KB |
Output is correct |
20 |
Correct |
25 ms |
49440 KB |
Output is correct |
21 |
Correct |
23 ms |
49576 KB |
Output is correct |
22 |
Correct |
24 ms |
49620 KB |
Output is correct |
23 |
Correct |
22 ms |
49576 KB |
Output is correct |
24 |
Correct |
24 ms |
49620 KB |
Output is correct |
25 |
Correct |
21 ms |
49616 KB |
Output is correct |
26 |
Correct |
25 ms |
49648 KB |
Output is correct |
27 |
Correct |
26 ms |
49604 KB |
Output is correct |
28 |
Correct |
26 ms |
49600 KB |
Output is correct |
29 |
Correct |
25 ms |
49572 KB |
Output is correct |
30 |
Correct |
21 ms |
49592 KB |
Output is correct |
31 |
Correct |
24 ms |
49580 KB |
Output is correct |
32 |
Correct |
22 ms |
49620 KB |
Output is correct |
33 |
Correct |
22 ms |
49564 KB |
Output is correct |
34 |
Correct |
22 ms |
49400 KB |
Output is correct |
35 |
Correct |
20 ms |
49356 KB |
Output is correct |
36 |
Correct |
21 ms |
49432 KB |
Output is correct |
37 |
Correct |
21 ms |
49444 KB |
Output is correct |
38 |
Correct |
20 ms |
49516 KB |
Output is correct |
39 |
Correct |
21 ms |
49580 KB |
Output is correct |
40 |
Correct |
30 ms |
50456 KB |
Output is correct |
41 |
Correct |
213 ms |
50960 KB |
Output is correct |
42 |
Correct |
54 ms |
53708 KB |
Output is correct |
43 |
Correct |
52 ms |
53716 KB |
Output is correct |
44 |
Correct |
50 ms |
53788 KB |
Output is correct |
45 |
Correct |
53 ms |
53768 KB |
Output is correct |
46 |
Correct |
48 ms |
53856 KB |
Output is correct |
47 |
Correct |
59 ms |
51600 KB |
Output is correct |
48 |
Correct |
52 ms |
52684 KB |
Output is correct |
49 |
Correct |
47 ms |
53056 KB |
Output is correct |
50 |
Correct |
63 ms |
53232 KB |
Output is correct |
51 |
Correct |
54 ms |
52624 KB |
Output is correct |
52 |
Correct |
52 ms |
52648 KB |
Output is correct |
53 |
Correct |
51 ms |
53800 KB |
Output is correct |
54 |
Correct |
236 ms |
70288 KB |
Output is correct |
55 |
Execution timed out |
3082 ms |
68292 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
49364 KB |
Output is correct |
2 |
Correct |
22 ms |
49400 KB |
Output is correct |
3 |
Correct |
21 ms |
49392 KB |
Output is correct |
4 |
Correct |
21 ms |
49364 KB |
Output is correct |
5 |
Correct |
21 ms |
49400 KB |
Output is correct |
6 |
Correct |
22 ms |
49352 KB |
Output is correct |
7 |
Correct |
23 ms |
49372 KB |
Output is correct |
8 |
Correct |
21 ms |
49364 KB |
Output is correct |
9 |
Correct |
22 ms |
49356 KB |
Output is correct |
10 |
Correct |
21 ms |
49452 KB |
Output is correct |
11 |
Correct |
21 ms |
49452 KB |
Output is correct |
12 |
Correct |
21 ms |
49412 KB |
Output is correct |
13 |
Execution timed out |
3080 ms |
104680 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |