#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
string s[59];
int id[59];
bool vis[109][59][59];
long long d[109][59][59];
struct cus{
int diff,l,sf;
long long val;
bool operator < (const cus &p)const {
return val>p.val;
}
};
int n,m;
int bonus=50;
const long long inf=1e18;
long long f[59][59];
pii can[51][51];
void sp(){
priority_queue<cus>t;
for1(i,-50,50){
for1(j,0,n){
for1(k,0,m){
d[i+bonus][j][k]=inf;
}
}
}
for1(i,1,n){
for1(j,1,n){
if (i==j)continue;
int m1=sz(s[id[i]]),m2=sz(s[id[j]]);
if (f[i][min(m1,m2)]==f[j][min(m1,m2)]){
if (m1==m2){
d[0+bonus][0][0]=m1;
t.push({0,0,0,m1});
}
else {
if (m1<m2){
t.push({m1-m2,j,m2-m1,m2});
d[m1-m2+bonus][j][m2-m1]=m2;
}
else {
t.push({m1-m2,i,m1-m2,m1});
d[m1-m2+bonus][i][m1-m2]=m1;
}
}
}
}
}
while (!t.empty()){
auto u=t.top();
t.pop();
if (vis[u.diff+bonus][u.l][u.sf])continue;
vis[u.diff+bonus][u.l][u.sf]=1;
//cout<<u.diff<<" "<<u.l<<" "<<u.sf<<'\n';
if (u.l==0)continue;
if (u.diff==0)continue;
long long prefix=f[u.l][sz(s[id[u.l]])];
int need=sz(s[id[u.l]])-u.sf;
prefix/=(1ll<<need);
//cout<<u.diff<<" "<<u.l<<" "<<u.sf<<" "<<u.val<<'\n';
for1(i,1,n){
int len=sz(s[id[i]]);
if (f[i][min(len,u.sf)]!=(prefix)%(1ll<<(min(len,u.sf))))continue;
if (len==u.sf){
if (d[0+bonus][0][0]>u.val){
d[0+bonus][0][0]=u.val;
t.push({0,0,0,u.val});
}
}
else if (len>u.sf){
int newdiff=u.diff;
if (u.diff<0)newdiff+=len;
else newdiff-=len;
if (d[newdiff+bonus][i][len-u.sf]>u.val+len-abs(u.diff)){
d[newdiff+bonus][i][len-u.sf]=u.val+len-abs(u.diff);
t.push({newdiff,i,len-u.sf,u.val+len-abs(u.diff)});
}
}
else{
int newdiff=u.diff;
if (u.diff<0)newdiff+=len;
else newdiff-=len;
if (d[newdiff+bonus][u.l][u.sf-len]>u.val){
d[newdiff+bonus][u.l][u.sf-len]=u.val;
t.push({newdiff,u.l,u.sf-len,u.val});
}
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("CODE.OUT","w",stdout);
cin>>n>>m;
for1(i,1,n){
id[i]=i;
cin>>s[i];
}
for1(i,1,n){
for1(j,0,m-1){
if (sz(s[id[i]])<j+1){
f[i][j+1]=-1;
}
else {
f[i][j+1]=f[i][j];
if (s[id[i]][j]=='1'){
f[i][j+1]+=(1ll<<j);
}
}
}
}
for1(i,1,n){
int len=sz(s[id[i]]);
for1(j,1,len){
//can[i][j]
int xd=len-j;
long long mask=f[i][len]/(1ll<<xd);
for1(k,1,n){
if (sz(s[id[k]])<j)continue;
if (f[k][j]==mask){
if (!can[i][j].fi){
can[i][j].fi=k;
}
can[i][j].se=k;
}
}
}
}
sp();
long long best=d[0+bonus][0][0];
if (best>=1e18){
cout<<-1;
}
else cout<<best;
cout<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
968 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1000 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
976 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1364 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
2 ms |
1748 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
3 ms |
3276 KB |
Output is correct |
11 |
Correct |
1 ms |
2260 KB |
Output is correct |
12 |
Correct |
1 ms |
1996 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
2 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
968 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1000 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
976 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
956 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1748 KB |
Output is correct |
32 |
Correct |
2 ms |
1876 KB |
Output is correct |
33 |
Correct |
1 ms |
1748 KB |
Output is correct |
34 |
Correct |
2 ms |
1748 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
3 ms |
3276 KB |
Output is correct |
37 |
Correct |
1 ms |
2260 KB |
Output is correct |
38 |
Correct |
1 ms |
1996 KB |
Output is correct |
39 |
Correct |
1 ms |
1620 KB |
Output is correct |
40 |
Correct |
1 ms |
1620 KB |
Output is correct |
41 |
Correct |
2 ms |
1620 KB |
Output is correct |
42 |
Correct |
3 ms |
3412 KB |
Output is correct |
43 |
Correct |
1 ms |
2132 KB |
Output is correct |
44 |
Correct |
1 ms |
1620 KB |
Output is correct |
45 |
Correct |
3 ms |
3388 KB |
Output is correct |
46 |
Correct |
2 ms |
2900 KB |
Output is correct |
47 |
Correct |
3 ms |
3284 KB |
Output is correct |
48 |
Correct |
1 ms |
1748 KB |
Output is correct |
49 |
Correct |
2 ms |
3412 KB |
Output is correct |
50 |
Correct |
2 ms |
2516 KB |
Output is correct |
51 |
Correct |
2 ms |
3152 KB |
Output is correct |
52 |
Correct |
2 ms |
3156 KB |
Output is correct |
53 |
Correct |
2 ms |
3156 KB |
Output is correct |
54 |
Correct |
2 ms |
2900 KB |
Output is correct |
55 |
Correct |
3 ms |
3284 KB |
Output is correct |
56 |
Correct |
1 ms |
1620 KB |
Output is correct |
57 |
Correct |
1 ms |
2260 KB |
Output is correct |
58 |
Correct |
1 ms |
2388 KB |
Output is correct |
59 |
Correct |
2 ms |
3284 KB |
Output is correct |
60 |
Correct |
2 ms |
3028 KB |
Output is correct |
61 |
Correct |
2 ms |
2388 KB |
Output is correct |
62 |
Correct |
2 ms |
2644 KB |
Output is correct |
63 |
Correct |
2 ms |
2632 KB |
Output is correct |
64 |
Correct |
2 ms |
2764 KB |
Output is correct |
65 |
Correct |
3 ms |
3412 KB |
Output is correct |
66 |
Correct |
3 ms |
3156 KB |
Output is correct |
67 |
Correct |
3 ms |
3412 KB |
Output is correct |
68 |
Correct |
3 ms |
3284 KB |
Output is correct |