Problem:
Ambiguous Encoding
A friend of yours is designing an encoding scheme of a set of characters into a set of variable length bit sequences. You are asked to check whether the encoding is ambiguous or not. In an encoding scheme, characters are given distinct bit sequences of possibly different lengths as their codes. A character sequence is encoded into a bit sequence which is the concatenation of the codes of the characters in the string in the order of their appearances. An encoding scheme is said to be ambiguous if there exist two different character sequences encoded into exactly the same bit sequence. Such a bit sequence is called an “ambiguous binary sequence”.
For example, encoding characters “
A
”, “
B
”, and “
C
” to
0
,
01
and
10
, respectively, is ambiguous. This scheme encodes two different character strings “
AC
” and “
BA
” into the same bit sequence
010
.
Input
The input consists of a single test case of the following format.
$n$
$w_1$
.
.
.
$w_n$
Here, $n$ is the size of the set of characters to encode ($1 \leq n \leq 1000$). The $i$-th line of the following $n$ lines, $w_i$, gives the bit sequence for the $i$-th character as a non-empty sequence of at most 16 binary digits, 0 or 1. Note that different characters are given different codes, that is, $w_i \ne w_j$ for $i \ne j$.
Output
If the given encoding is ambiguous, print in a line the number of bits in the shortest ambiguous binary sequence. Output zero, otherwise.
Sample Input 1
3
0
01
10
Sample Output 1
3
Sample Input 2
3
00
01
1
Sample Output 2
0
Sample Input 3
3
00
10
1
Sample Output 3
0
Sample Input 4
10
1001
1011
01000
00011
01011
1010
00100
10011
11110
0110
Sample Output 4
13
Sample Input 5
3
1101
1
10
Sample Output 5
4

Submitted solutions:

code_snippet: #include<bits/extc++.h>
#define rep(i,n) for(int i = 0; i< (n); i++)
#define all(a) begin(a),end(a)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 998244353;
const int inf = (1<<30);
const ll INF = (1ull<<62);
const int dx[8] = {1,0,-1,0,1,1,-1,-1};
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b){ return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b){ return a > b && (a = b, true);}

P f(P x, P y){
    if(x.second > y.second) swap(x,y);
    rep(i,x.second){
        if( ((x.first>>i)&1) != ((y.first>>i)&1)){
            return {-1,-1};
        }
    }
    return make_pair((y.first>>(x.second)), y.second - x.second);
}


int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout.setf(ios_base::fixed);
    cout.precision(12);
    int n; cin>>n;
    vector<vector<ll>> g((1<<17),vector<ll>(18,inf));
    
    vector<P> w(n);
    rep(i,n){
        string s; cin>>s;
        w[i] = {stoi(s,0,2),s.size()};
    }
    priority_queue<pair<int,P>,vector<pair<int,P>>, greater<pair<int,P>> > q;
    for(int i = 0; i<n; i++){
        for(int j = i+1; j<n; j++){
            P x = f(w[i],w[j]);
            //cout<<w[j].first<<" "<<w[i].first<<endl;
            if(x.second == -1) continue;
            if(chmin(g[x.first][x.second],max(w[i].second,w[j].second) - x.second)){
                //cout<<x.first<<" "<<x.second<<" "<<i<<" "<<j<<endl;
                q.push({g[x.first][x.second],{x.first,x.second}});
            }
        }
    }

    while(q.size()){
        auto [c,y] = q.top(); q.pop();
        if(g[y.first][y.second] < c) continue;
        //cout<<"Y "<<c<<" "<<y.first<<" "<<y.second<<endl;

        for(int i = 0; i<n; i++){
            P x = f(y,w[i]);

            if(x.second == -1) continue;
            if(chmin(g[x.first][x.second],c + max(w[i].second,y.second) - x.second)){
                q.push({g[x.first][x.second],{x.first,x.second}});
            }
        }
    }
    if(g[0][0] == inf) cout<<0<<endl;
    else cout<<g[0][0]<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 26760
final_rank: 7

code_snippet: #include<bits/extc++.h>
#define rep(i,n) for(int i = 0; i< (n); i++)
#define all(a) begin(a),end(a)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 998244353;
const int inf = (1<<30);
const ll INF = (1ull<<62);
const int dx[8] = {1,0,-1,0,1,1,-1,-1};
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b){ return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b){ return a > b && (a = b, true);}

P f(P x, P y){
    if(x.second > y.second) swap(x,y);
    rep(i,x.second){
        if( ((x.first>>i)&1) != ((y.first>>i)&1)){
            return {-1,-1};
        }
    }
    return make_pair((y.first>>(x.second)), y.second - x.second);
}


int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout.setf(ios_base::fixed);
    cout.precision(12);
    int n; cin>>n;
    vector<vector<ll>> g((1<<17),vector<ll>(18,inf));
    
    vector<P> w(n);
    rep(i,n){
        string s; cin>>s;
        w[i] = {stoi(s,0,2),s.size()};
    }
    priority_queue<pair<int,P>,vector<pair<int,P>>, greater<pair<int,P>> > q;
    for(int i = 0; i<n; i++){
        for(int j = i+1; j<n; j++){
            P x = f(w[i],w[j]);
            //cout<<w[j].first<<" "<<w[i].first<<endl;
            if(x.second == -1) continue;
            if(chmin(g[x.first][x.second],max(w[i].second,w[j].second) - x.second)){
                //cout<<x.first<<" "<<x.second<<" "<<i<<" "<<j<<endl;
                q.push({g[x.first][x.second],{x.first,x.second}});
            }
        }
    }

    while(q.size()){
        auto [c,y] = q.top(); q.pop();
        if(g[0][0] != inf) break;
        if(g[y.first][y.second] < c) continue;
        //cout<<"Y "<<c<<" "<<y.first<<" "<<y.second<<endl;

        for(int i = 0; i<n; i++){
            P x = f(y,w[i]);

            if(x.second == -1) continue;
            if(chmin(g[x.first][x.second],c + max(w[i].second,y.second) - x.second)){
                q.push({g[x.first][x.second],{x.first,x.second}});
            }
        }
    }
    if(g[0][0] == inf) cout<<0<<endl;
    else cout<<g[0][0]<<endl;
}
accuracy: 0.7301587301587301
time_ms: 10
memory_kb: 26784
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vl vector<ll>
#define vvl vector<vl>
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define loop(i,m, n) for(ll i = m; i <= n; ++i)
#define inf 1LL << 60

//グラフgの頂点startからの最短経路を全ての頂点に対して求める。
vl dijkstra(vector<vector<pair<ll,ll>>> g,ll start){
	priority_queue<pair<ll,ll>> dj;
	vl cost(g.size(),inf);
	cost[start]=0;
	dj.push({start,0});
	while(!dj.empty()){
		ll nowcost=-dj.top().first;
		ll tmp=dj.top().second;
		dj.pop();
		if(cost[tmp]<nowcost)continue;
		rep(i,g[tmp].size()){
			if(cost[g[tmp][i].first]>nowcost+g[tmp][i].second){
				cost[g[tmp][i].first]=nowcost+g[tmp][i].second;
				dj.push({-cost[g[tmp][i].first],g[tmp][i].first});
			}
		}
	}
	return cost;
}

//どちらかがどちらかの接頭辞になっていなければ、"error"を返す
//なっていれば、マッチしなかった残りの文字を返す
//マッチ後の文字列がinputに含まれてたらgoalを返す
set<string> input;

string matching (string s,string t){
	if(s.size()>t.size())swap(s,t);
	bool f=true;
	rep(i,s.size()){
		if(s[i]!=t[i]){
			f=false;
			break;
		}
	}
	if(f){
		string ans=t.substr(s.size());
		if(input.count(ans))return "goal";
		else return ans;
	}else{
		 return "error";
	}
}

int main(){
    ll n;
    cin >> n;
    vector<string> s(n);
    
	//グラフの頂点と番号の対応を作る(全ての入力の部分文字列に対して)
	ll mpcnt=0;
	map<string,ll> mp;
	mp["start"]=mpcnt;
	mpcnt++;
    rep(i, n){
        cin >> s[i];
        input.insert(s[i]);

		//部分文字列しか頂点に成り得ない/部分文字列は少ない
		rep(j,s[i].size()){
			rep(k,j+1){
				string tmp;
				loop(l,k,j)tmp.push_back(s[i][l]);
				if(!mp.count(tmp)){
					
					mp[tmp]=mpcnt;
					mpcnt++;
				}
			}
		}
    }


	mp["goal"]= mpcnt;
	mpcnt++;

	//グラフを実際に構築(全ての頂点に対して入力文字のmatchingを考える)
	vector<vector<pair<ll,ll>>> g(mpcnt);
	for(const auto & val:mp){
		rep(i,n){
			if(val.first==s[i])continue;
			string tmp=matching(val.first,s[i]);
			if(tmp=="error")continue;
			
			ll cost;
			if(val.first.size()<s[i].size()){
				if(tmp=="goal")cost=s[i].size()-val.first.size();
				else cost=tmp.size();
			}else{
				cost=0;
			}

			g[val.second].push_back({mp[tmp],cost});
		}
	}

	rep(i,n){
		g[0].push_back({mp[s[i]],s[i].size()});
	}

	//作り終わったグラフにダイクストラ関数を呼んで適応。
	vl ans = dijkstra(g,0);
	if(ans.back()!=inf)cout<<ans.back()<<endl;
	else cout<<0<<endl;
}
accuracy: 1
time_ms: 550
memory_kb: 5764
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vl vector<ll>
#define vvl vector<vl>
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define loop(i,m, n) for(ll i = m; i <= n; ++i)
#define inf 1LL << 60

//グラフgの頂点startからの最短経路を全ての頂点に対して求める。
vl dijkstra(vector<vector<pair<ll,ll>>> g,ll start){
	priority_queue<pair<ll,ll>> dj;
	vl cost(g.size(),inf);
	cost[start]=0;
	dj.push({start,0});
	while(!dj.empty()){
		ll nowcost=-dj.top().first;
		ll tmp=dj.top().second;
		dj.pop();
		if(cost[tmp]<nowcost)continue;
		rep(i,g[tmp].size()){
			if(cost[g[tmp][i].first]>nowcost+g[tmp][i].second){
				cost[g[tmp][i].first]=nowcost+g[tmp][i].second;
				dj.push({-cost[g[tmp][i].first],g[tmp][i].first});
			}
		}
	}
	return cost;
}

//どちらかがどちらかの接頭辞になっていなければ、"error"を返す
//なっていれば、マッチしなかった残りの文字を返す
//マッチ後の文字列がinputに含まれてたらgoalを返す
set<string> input;

string matching (string s,string t){
	if(s.size()>t.size())swap(s,t);
	bool f=true;
	rep(i,s.size()){
		if(s[i]!=t[i]){
			f=false;
			break;
		}
	}
	if(f){
		string ans=t.substr(s.size());
		if(input.count(ans))return "goal";
		else return ans;
	}else{
		 return "error";
	}
}

int main(){
    ll n;
    cin >> n;
    vector<string> s(n);
    
	//グラフの頂点と番号の対応を作る(全ての入力の部分文字列に対して)
	ll mpcnt=0;
	map<string,ll> mp;
	mp["start"]=mpcnt;
	mpcnt++;
    rep(i, n){
        cin >> s[i];
        input.insert(s[i]);

		//部分文字列しか頂点に成り得ない/部分文字列は少ない
		rep(j,s[i].size()){
			rep(k,j+1){
				string tmp;
				loop(l,k,j)tmp.push_back(s[i][l]);
				if(!mp.count(tmp)){
					
					mp[tmp]=mpcnt;
					mpcnt++;
				}
			}
		}
    }


	mp["goal"]= mpcnt;
	mpcnt++;

	//グラフを実際に構築(全ての頂点に対して入力文字のmatchingを考える)
	vector<vector<pair<ll,ll>>> g(mpcnt);
	for(const auto & val:mp){
		rep(i,n){
			if(val.first==s[i])continue;
			string tmp=matching(val.first,s[i]);
			if(tmp=="error")continue;
			
			ll cost;
			if(val.first.size()<s[i].size()){
				if(tmp=="goal")cost=s[i].size()-val.first.size();
				else cost=tmp.size();
			}else{
				cost=0;
			}

			g[val.second].push_back({mp[tmp],cost});
		}
	}

	rep(i,n){
		g[0].push_back({mp[s[i]],s[i].size()});
	}

	//作り終わったグラフにダイクストラ関数を呼んで適応。
	vl ans = dijkstra(g,0);
	cout<<ans.back()<<endl;
}
accuracy: 0.1746031746031746
time_ms: 80
memory_kb: 4448
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 2000005
set<pii> dp[1005][1005];
int main(){
    int n;
    scanf("%d",&n);
    char c[1005][20];
    int len[1005];
    vector<int> v[2];
    for(int i =0;i<n;i++){
        scanf("%s",c[i]);
        v[c[i][0]-'0'].pb(i);
        len[i]=strlen(c[i]);
    }
    queue<pair<int,pair<pii,pii> > > q;
    for(int t=0;t<2;t++){
        for(int i = 0;i<v[t].size();i++){
            for(int j=i+1;j<v[t].size();j++){
                q.push(mp(1,mp(mp(v[t][i],0),mp(v[t][j],0))));
                dp[v[t][i]][v[t][j]].insert(mp(0,0));
            }
        }
    }
    while(!q.empty()){
        auto p=q.front();
        q.pop();
    //    printf("%d %d %d %d %d\n",p.y.x.x,p.y.x.y,p.y.y.x,p.y.y.y,p.x);
        if(len[p.y.x.x]-1==p.y.x.y&&len[p.y.y.x]-1==p.y.y.y){
            printf("%d\n",p.x);
            return 0;
        }
        else if(len[p.y.x.x]-1==p.y.x.y)
        {
            p.y.y.y++;
            p.x++;
            for(auto it:v[c[p.y.y.x][p.y.y.y]-'0']){
                int a=it,b=0;
                int tag=0;
                if(a>p.y.y.x){
                    swap(p.y.y.x,a);
                    swap(p.y.y.y,b);
                    tag=1;
                }
                if(dp[a][p.y.y.x].find(mp(b,p.y.y.y))==dp[a][p.y.y.x].end()){
                    dp[a][p.y.y.x].insert(mp(b,p.y.y.y));
                    q.push(mp(p.x,mp(mp(a,b),mp(p.y.y.x,p.y.y.y))));
                }
                if(tag){
                    swap(p.y.y.x,a);
                    swap(p.y.y.y,b);
                }
            }
        }
        else if(len[p.y.y.x]-1==p.y.y.y){
            p.y.x.y++;
            p.x++;
            for(auto it:v[c[p.y.x.x][p.y.x.y]-'0']){
                int a=it,b=0;
                int tag=0;
                if(a<p.y.x.x){
                    swap(a,p.y.x.x);
                    swap(p.y.x.y,b);
                    tag=1;
                }
                if(dp[p.y.x.x][a].find(mp(p.y.x.y,b))==dp[p.y.x.x][a].end()){
                    dp[p.y.x.x][a].insert(mp(p.y.x.y,b));
                    q.push(mp(p.x,mp(p.y.x,mp(a,b))));
                }
                if(tag){
                    swap(a,p.y.x.x);
                    swap(p.y.x.y,b);
                }
            }
        }
        else{
            if(c[p.y.y.x][p.y.y.y+1]==c[p.y.x.x][p.y.x.y+1]){
                p.x++;
                p.y.y.y++;
                p.y.x.y++;
                if( dp[p.y.y.x][p.y.x.x].find(mp(p.y.y.y,p.y.x.y))==dp[p.y.y.x][p.y.x.x].end()){
                    dp[p.y.y.x][p.y.x.x].insert(mp(p.y.y.y,p.y.x.y));
                    q.push(p);
                }
            }
        }
    }
    printf("0\n");
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 125444
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<string> s(n);
	for(int i = 0; i < n; ++i) cin >> s[i];
	vector<vector<vector<tuple<int, int, int>>>> G(n);
	for(int i = 0; i < n; ++i) {
		G[i].assign(s[i].size()+1, {});
	}
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			int si = s[i].size(), sj = s[j].size();
			for(int k = 0; k < si; ++k){
				if(si-k > sj) continue;
				if(i == j && k == 0) continue;
				if(s[i].substr(k, si-k) == s[j].substr(0, si-k)){
					G[i][k].emplace_back(j, si-k, si-k);
				}
			}
			if(si+1 < sj){
				for(int k = 1; k <= sj-si-1; ++k){
					if(s[i] == s[j].substr(k, si)){
						G[j][k].emplace_back(j, k+si, si);
					}
				}
			}
		}
	}
	//for(int i = 0; i < n; ++i){
	//	for(int j = 0; j < G[i].size(); ++j){
	//		for(auto [x, y, z]: G[i][j]){
	//			cout << i << ' ' << j << ' ' << x << ' ' << y << ' ' << z << endl;
	//		}
	//	}
	//}

	vector<vector<int>> seen(n);
	int INF = 1<<30;
	for(int i = 0; i < n; ++i){
		seen[i].assign(s[i].size()+1, INF);
	}
	priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;
	for(int i = 0; i < n; ++i){
		if(!G[i][0].empty()){
			pq.emplace(0, i, 0);
			seen[i][0] = 0;
		}
	}
	
	while(!pq.empty()){
		auto[c, i, j] = pq.top();
		pq.pop();
		//cout << c << i << j << endl;
		if(seen[i][j] != c) continue;
		for(auto [ni, nj, nc]: G[i][j]){
			if(seen[ni][nj] > c+nc){
				seen[ni][nj] = c+nc;
				pq.emplace(c+nc, ni, nj);
			}
		}
	}

	int ans = INF;
	for(int i = 0; i < n; ++i){
		ans = min(ans, seen[i].back());
	}
	if(ans == INF) ans = 0;
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 20608
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <cassert>
#include <algorithm>
int N;
std::string W[1010];
std::vector<std::string> comp;
std::string decode(int v) {
    assert(0 <= v and v < (int)comp.size());
    return comp[v];
}
int encode(const std::string& S) {
    auto it{std::lower_bound(comp.begin(), comp.end(), S)};
    assert(it != comp.end() and *it == S);
    return (int)(it - comp.begin());
}
int solve() {
    const int INF{(int)1e9};
    std::vector dp(comp.size(), INF);
    for (int i{} ; i < N ; i++) {
        for (int j{i + 1} ; j < N ; j++) {
            std::string S{W[i]}, T{W[j]};
            if (S.size() > T.size()) std::swap(S, T);
            bool ok{T.substr(0, S.size()) == S};
            if (!ok) continue;
            int key{encode(T.substr(S.size(), T.size() - S.size()))};
            // std::cout << T.substr(S.size(), T.size() - S.size()) << ' ' << S.size() << ' ' << key << std::endl;
            dp[key] = std::min(dp[key], (int)S.size());
        }
    }
    assert(dp[encode("")] == INF);
    using qt = std::pair<int, int>;
    std::priority_queue<qt, std::vector<qt>, std::greater<qt>> que;
    for (int i{} ; i < (int)dp.size() ; i++) if (dp[i] != INF) {
        que.push({dp[i], i});
    }
    while (que.size()) {
        auto [d, v]{que.top()};
        que.pop();
        if (dp[v] < d) continue;
        if (v == encode("")) continue;
        // std::cout << "check " << decode(v) << std::endl;
        for (int i{} ; i < N ; i++) {
            std::string S{decode(v)}, T{W[i]};
            if (S.size() > T.size()) std::swap(S, T);
            bool ok{T.substr(0, S.size()) == S};
            if (!ok) continue;
            int x{encode(T.substr(S.size(), T.size() -  S.size()))};
            // std::cout << "ok " << S << ' ' << T << ' ' << T.substr(S.size(), T.size() - S.size()) << ' ' << x << std::endl;
            if (dp[x] > d + (int)S.size()) {
                dp[x] = d + (int)S.size();
                que.push({dp[x], x});
            }
        }
    }
    int key{encode("")};
    return (dp[key] == INF ? 0 : dp[key]);
}
int main() {
    for (int i{} ; i <= 16 ; i++) {
        for (int bit{} ; bit < (1 << i) ; bit++) {
            std::string S;
            for (int j{} ; j < i ; j++) {
                S += (bit & (1 << j) ? '1' : '0');
            }
            comp.push_back(S);
        }
    }
    std::sort(comp.begin(), comp.end());
    std::cin >> N;
    for (int i{} ; i < N ; i++) std::cin >> W[i];
    std::cout << solve() << '\n';
}
accuracy: 1
time_ms: 60
memory_kb: 9936
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define ALL(v) v.begin(),v.end()
#define dbg(x) cerr << #x << ": " << (x) << endl;
int n;
vector<string> w;

inline string prefix(string mask, int x) {
    return mask.substr(0,x);
}

inline string suffix(string mask, int x) {
    return mask.substr(mask.size()-x);
}

int main() {
    cin >> n;
    w.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> w[i];
    }
    map<string,int> id;
    id[""];
    for (int i = 0; i < n; ++i) {
        for (int j = 1; j <= w[i].size(); ++j) {
            id[suffix(w[i], j)];
        }
    }
    int idx = 0;
    for (auto& [key,val] : id) {
        val = idx++;
    }

    int m = id.size();
    vector<vector<pair<int,int>>> g(m);
    for (auto& [key,val] : id) {
        for (auto& str : w) {
            if (key.size() == str.size()) {
                if (key == str) g[val].emplace_back(0, key.size());
            }
            else if (key.size() < str.size()) {
                if (prefix(str, key.size()) == key) g[val].emplace_back(id[suffix(str, str.size() - key.size())], key.size());
            }
            else {
                if (prefix(key, str.size()) == str) g[val].emplace_back(id[suffix(key, key.size() - str.size())], str.size());
            }
        }
    }
    int inf = 1e9;
    vector<int> dis(m, inf);
    priority_queue<pair<int,int>> pq;
    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j < n; ++j) {
            string t;
            int p = 0;
            if (w[i].size() <= w[j].size() && prefix(w[j], w[i].size()) == w[i]) {
                t = suffix(w[j], w[j].size()-w[i].size());
                p = w[i].size();
            }
            else if (w[j].size() <= w[i].size() && prefix(w[i], w[j].size()) == w[j]) {
                t = suffix(w[i], w[i].size()-w[j].size());
                p = w[j].size();
            }
            else {
                continue;
            }
            // cerr << w[i] << ' ' << w[j] << ' ' << t << '\n';

            int k = id[t];
            dis[k] = p;
            pq.emplace(-p,k);
        }
    }
    while (pq.size()) {
        auto [cost,pos] = pq.top();
        pq.pop();
        cost = -cost;
        if (cost > dis[pos]) continue;
        for (auto [to,c] : g[pos]) if (dis[pos] + c < dis[to]) {
            dis[to] = dis[pos] + c;
            pq.emplace(-dis[to], to);
        }
    }
    // for (auto [key,val] : id) {
    //     cerr << key << ": " << dis[val] << '\n';
    // }
    int ans = dis[0];
    if (ans == inf) ans = 0;
    cout << ans << '\n';
}
accuracy: 1
time_ms: 130
memory_kb: 4108
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define ALL(obj) (obj).begin(),(obj).end()

ll n,siz[1000],num[1000],dp[17][1<<17];
string str[1000];
priority_queue<vll,vector<vll>,greater<vll>>pq;
set<pair<ll,ll>>s;

int main(){
	for(int i=0;i<17;i++) for(int S=0;S<(1<<17);S++) dp[i][S] = 1e17;

	cin >> n;
	for(int i=0;i<n;i++)
	{
		cin >> str[i];
		siz[i] = str[i].size();
		for(int j=0;j<siz[i];j++)
		{
			if(str[i][siz[i]-1-j]=='1')
			{
				num[i] += 1<<j;
			}
		}
		pq.push({siz[i],siz[i],num[i],1});
		s.insert({siz[i],num[i]});
	}

	while(!pq.empty())
	{
		auto state = pq.top();
		pq.pop();

		ll ccost = state[0];
		ll clen = state[1];
		ll cnum = state[2];
		ll flag = state[3];
		//cout << ccost << " " << clen << " " << cnum << endl;
		if(!flag && s.count({clen,cnum})) dp[0][0] = min(dp[0][0], ccost);
		if(dp[clen][cnum]<=ccost)
		{
			continue;
		}
		dp[clen][cnum] = ccost;

		for(int i=0;i<n;i++)
		{
			if(clen + siz[i] < 17)
			{
				//cout << "#" << dp[clen][cnum] + siz[i] << " " << siz[i] + clen << " " << (num[i] << clen) + cnum << endl;
				pq.push({dp[clen][cnum] + siz[i], siz[i] + clen, (num[i] << clen) + cnum, 0});
			}
			if(clen <= siz[i])
			{
				if(flag && clen==siz[i]) continue;

				if((num[i] & ((1<<clen)-1)) == cnum)
				{
					//cout << "!" << dp[clen][cnum] + siz[i] - clen << " " << siz[i]-clen << " " << (num[i] >> clen) << endl;
					pq.push({dp[clen][cnum] + siz[i] - clen, siz[i]-clen, num[i] >> clen, 0});
				}
			}
		}
	}

	if(dp[0][0]==1e17) cout << 0 << endl;
	else cout << dp[0][0] << endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 24944
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define srep(i, s, n) for(int i = (s); i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
using i64 = long long;
using f64 = long double;
i64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) <  0 && n % d != 0); }
i64  ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }

int main() {
    map<string, int> encode;
    vector<string> decode;
    int V = 0;
    rep(s, 16) {
        rep(x, 1 << s) {
            string str = "";
            rep(i, s) str += (x & (1 << i) ? '1' : '0');
            encode[str] = V++;
            decode.push_back(str);
        }
    }

    int n; cin >> n;
    vector<string> w(n);
    rep(i, n) cin >> w[i];
    vector<vector<pair<int,int>>> G(V);
    for(const string& str : w) {
        for(int s = 0; s <= len(str); s++) {
            const int u = encode[str.substr(0, s)];
            const int v = encode[str.substr(s)];
            G[u].push_back({v, len(str) - s});
        }

        for(int s = 1; s <= 15 - len(str); s++) {
            rep(x, 1 << s) {
                string add = "";
                rep(i, s) add += (x & (1 << i) ? '1' : '0');
                const int u = encode[str + add];
                const int v = encode[add];
                G[u].push_back({v, 0});
            }
        }
    }

    const int INF = 1e9;
    vector<int> dist1(V, INF), dist2(V, INF);
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
    q.push({0, 0});
    while(not q.empty()) {
        auto [dv, v] = q.top(); q.pop();
        if(dist1[v] == INF) {
            dist1[v] = dv;
        } else if(dist2[v] == INF) {
            dist2[v] = dv;
            if(v == 0) {
                cout << dv << endl;
                return 0;
            }
        } else continue;

        for(auto [to, c] : G[v]) {
            if(dv == len(decode[v]) and to == 0) continue;
            q.push({dv + c, to});
        }
    }

    cout << 0 << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 14304
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

template<class T>
bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}

ll N;
vector<string> vec;

array<ll, 2> ERROR = {-1, -1};
array<ll, 2> T;
map< array<ll, 2>, vector<array<ll, 3>> > graph;

void construct(array<ll, 2> from) {
  auto [v, x] = from;
  string s = vec[v].substr(x);
  
  for (int nv = 0; nv < N; ++nv) {
    if (x == 0 && v == nv) continue;
    
    string t = vec[nv];
    if (s == t) {
      graph[from].push_back({N, 0, (ll)s.length()});
      continue;
    }
    
    if (s.length() < t.length()) {
      bool flg = true;
      for (int i = 0; i < s.length(); ++i) {
        if (s[i] != t[i]) flg = false;
      }
      if (flg) {
        graph[from].push_back({nv, (ll)s.length(), (ll)s.length()});
      }
    }
    else {
      bool flg = true;
      for (int i = 0; i < t.length(); ++i) {
        if (s[i] != t[i]) flg = false;
      }
      if (flg) {
        graph[from].push_back({v, x + (ll)t.length(), (ll)t.length()});
      }
    }
    
  }
  
  return;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    string w; cin >> w;
    vec.emplace_back(w);
  }
  
  T = {N, 0};
  for (ll i = 0; i < N; ++i) {
    for (ll x = 0; x < vec[i].length(); ++x) {
      construct({i, x});
      
      // cout << -1 << " " << i << " " << x << endl;
      // for (auto [v, y, w] : graph[{i, x}]) {
      //   cout << v << " " << y << " " << w << endl;
      // }
    }
  }
  
  priority_queue< array<ll, 3>, vector<array<ll, 3>>, greater<array<ll, 3>> > pq;
  map< array<ll, 2>, ll> dist;
  const ll INF = 1e9;
  for (int i = 0; i < N; ++i) {
    for (int x = 0; x < vec[i].length(); ++x) {
      dist[{i, x}] = INF;
    }
  }
  dist[{T}] = INF;
  
  for (int i = 0; i < N; ++i) {
    dist[{i, 0}] = 0;
    pq.push({0, i, 0});
  }
  
  while (pq.size()) {
    auto [d, v, x] = pq.top();
    pq.pop();
    
    if (dist[{v, x}] < d) continue;
    
    for (auto [nv, y, w] : graph[{v, x}]) {
      if (chmin(dist[{nv, y}], dist[{v, x}] + w)) {
        pq.push({dist[{nv, y}], nv, y});
      }
    }
  }
  
  cout << (dist[T] < INF ? dist[T] : 0) << endl;
  
  return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 35968
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll N;
    cin>>N;
    vector<string> S(N);
    for(auto &s:S)cin>>s;
    vector<vector<ll>> seen(N,vector<ll>(17,1e18));
    priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>> Q;
    for(int i=0;i<N;i++){
        Q.push({S[i].size(),{i,0}});
        seen[i][0]=S[i].size();
    }
    ll an=1e18;
    while(!Q.empty()){
        ll c=Q.top().first;
        int i=Q.top().second.first;
        int j=Q.top().second.second;
        Q.pop();
        int u=S[i].size()-j;
        if(c!=seen[i][j])continue;
        for(int n=0;n<N;n++){
            int v=S[n].size();
            int m=min(u,v);
            if(S[i].substr(j,m)!=S[n].substr(0,m))continue;
            if(u==v&&n!=i)an=min(an,c);
            else if(u<v){
                ll nc=c+v-u;
                if(seen[n][u]>nc){
                    seen[n][u]=nc;
                    Q.push({nc,{n,u}});
                }
            }
            else if(u>v&&seen[i][j+v]>c){
                seen[i][j+v]=c;
                Q.push({c,{i,j+v}});
            }
        }
    }
    cout<<(an<1e17?an:0)<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3644
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;
int main() {
    ll N;
    cin>>N;
    vector<string> S(N);
    for(auto &s:S)cin>>s;
    vector<vector<ll>> seen(N,vector<ll>(17,1e18));
    priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>> Q;
    for(int i=0;i<N;i++){
        Q.push({S[i].size(),{i,0}});
        seen[i][0]=S[i].size();
    }
    ll an=1e18;
    while(!Q.empty()){
        ll c=Q.top().first;
        int i=Q.top().second.first;
        int j=Q.top().second.second;
        Q.pop();
        int u=S[i].size();
        if(c!=seen[i][j])continue;
        
        for(int n=0;n<N;n++){
            int v=S[n].size();
            if(u-j==v){
                if(n!=i&&S[n]==S[i].substr(j,v)){
                    an=min(an,c);
                }
            }
            else if(u-j<v){
                if(S[n].substr(0,u-j)==S[i].substr(j,u-j)){
                    ll nc=c+v-(u-j);
                    if(seen[n][(u-j)]>nc){
                        seen[n][u-j]=nc;
                        Q.push({nc,{n,u-j}});
                    }
                }
            }
            else{
                if(S[n]==S[i].substr(j,v)){
                    if(seen[i][j+v]>c){
                        seen[i][j+v]=c;
                        Q.push({c,{i,j+v}});
                    }
                }
            }
        }
    }
    cout<<(an<1e17?an:0)<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3644
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll maxn=2e4, inf=1e18;

unordered_map<ll,ll> lg;

ll v[1005], id[1005][17], dis[maxn], vis[maxn];
string s[1005];
vector<pair<ll,ll> > g[maxn];

ll lowbit(ll x) {
	return x&(-x);
}

struct node {
	ll x;
	ll v;
	bool operator < (const node&other) const{
		return v>other.v;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	ll n,tot=0; cin>>n;
	for(ll i=1;i<=16;i++) lg[1<<i]=i;
	for(ll i=1;i<=n;i++) {
		cin>>s[i];
		for(ll j=0;j<s[i].size();j++) v[i]+=((s[i][j]-'0')<<j);
		for(ll j=0;j<=s[i].size();j++) id[i][j]=++tot;
	}
	for(ll i=1;i<=n;i++) {
		for(ll j=0;j<s[i].size();j++) {
			ll len=s[i].size()-j;
			ll val=(v[i]>>j);
			for(ll k=1;k<=n;k++) {
				if(!j&&k==i) continue;
				ll x=min(min(lg[lowbit(val^v[k])==0?(1<<16):lowbit(val^v[k])],len),(ll)s[k].size());
				if(x<len&&x<s[k].size()) continue;
				else if(x<len) g[id[i][j]].push_back({id[i][j+x],x});
				else g[id[i][j]].push_back({id[k][x],x});
			}
		}
	}
	priority_queue<node> q;
	for(ll i=1;i<=tot;i++) dis[i]=inf;
	for(ll i=1;i<=n;i++) dis[id[i][0]]=0, q.push((node){id[i][0],0});
	while(q.size()) {
		ll x=q.top().x; q.pop();
		if(vis[x]) continue;
		vis[x]=1;
		for(auto p:g[x]) {
			ll y=p.first, w=p.second;
			if(dis[y]>dis[x]+w) dis[y]=dis[x]+w, q.push((node){y,dis[y]});
		}
	}
	ll ans=inf;
	for(ll i=1;i<=n;i++) ans=min(ans,dis[id[i][s[i].size()]]);
	if(ans!=inf) cout<<ans<<'\n';
	else cout<<"0\n";
	return 0;
}//
accuracy: 1
time_ms: 140
memory_kb: 25860
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define id(i,j) (((i)-1)*16+(j)) 
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=1005;
const int V=N*20+5;

bool mem1;

struct Edge {
	int nex,to,dis;
} edge[N*N*20];
int head[V],dis[V];
priority_queue<pii,vector<pii>,greater<pii> >q;
char s[N][20];
int len[N];
int n,elen,S,T;
bool vis[V];

void addedge(int u,int v,int w) {
	edge[++elen]=(Edge){head[u],v,w},head[u]=elen;
}
bool cmin(int &a,int b) { return a>b?a=b,1:0; }
void dijkstra() {
	for(int i=1;i<=T;++i)dis[i]=1e9;
	dis[S]=0,q.push(pii(0,S));
	while(q.size()) {
		int u=q.top().se; q.pop();
		if(vis[u]) continue; vis[u]=true;
		for(int e=head[u],v;v=edge[e].to,e;e=edge[e].nex)
			if(cmin(dis[v],dis[u]+edge[e].dis))
				q.push(pii(dis[v],v));
	}
}

bool mem2;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
//	cerr<<"Memory Cost: "<<(&mem2-&mem1)/1048576.<<endl;
	
	cin>>n,S=id(n+1,1),T=S+1;
	for(int i=1;i<=n;++i) 
		cin>>(s[i]+1),len[i]=strlen(s[i]+1);

	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j) {
			int h=1;
			while(h<=len[i]&&h<=len[j]&&s[i][h]==s[j][h]) ++h;
			if(h<=len[i]&&h<=len[j]) continue;
			
			if(len[i]==len[j])addedge(S,T,len[i]); 
			else if(len[i]<len[j])addedge(S,id(j,h),len[j]);
			else addedge(S,id(i,h),len[i]);
		}
		
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			for(int k=1;k<=len[i];++k) {
				int h=1;
				while(k+h-1<=len[i]&&h<=len[j]&&s[i][k+h-1]==s[j][h]) ++h;
				if(k+h-1<=len[i]&&h<=len[j]) continue;
				
				int li=len[i]-k+1,lj=len[j];
				if(li==lj) addedge(id(i,k),T,0);
				else if(li<lj) addedge(id(i,k),id(j,h),lj-li);
				else addedge(id(i,k),id(i,k+h-1),0);
			}
		
	dijkstra();
	if(dis[T]==1e9)cout<<0<<'\n';
	else cout<<dis[T]<<'\n';
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 15612
final_rank: 6

code_snippet: #include<bits/stdc++.h>
#define id(i,j) (((i)-1)*16+(j)) 
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N=1005;
const int V=N*20+5;

bool mem1;

struct Edge {
	int nex,to,dis;
} edge[N*N*20];
int head[V],dis[V];
priority_queue<pii,vector<pii>,greater<pii> >q;
char s[N][20];
int len[N];
int n,elen,S,T;
bool vis[V];

void addedge(int u,int v,int w) {
	edge[++elen]=(Edge){head[u],v,w},head[u]=elen;
}
bool cmin(int &a,int b) { return a>b?a=b,1:0; }
void dijkstra() {
	for(int i=1;i<=T;++i)dis[i]=1e9;
	dis[S]=0,q.push(pii(0,S));
	while(q.size()) {
		int u=q.top().se; q.pop();
		if(vis[u]) continue; vis[u]=true;
		for(int e=head[u],v;v=edge[e].to,e;e=edge[e].nex)
			if(cmin(dis[v],dis[u]+edge[e].dis))
				q.push(pii(dis[v],v));
	}
}

bool mem2;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
//	cerr<<"Memory Cost: "<<(&mem2-&mem1)/1048576.<<endl;
	
//	freopen("data.in","r",stdin);
	
	cin>>n,S=id(n+1,1),T=S+1;
	for(int i=1;i<=n;++i) 
		cin>>(s[i]+1),len[i]=strlen(s[i]+1);

	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j) {
			int h=1;
			while(h<=len[i]&&h<=len[j]&&s[i][h]==s[j][h]) ++h;
			
			if(h<=len[i]&&h<=len[j]) continue;
			
			if(len[i]==len[j])addedge(S,T,len[i]); 
			else if(len[i]<len[j])addedge(S,id(j,h),len[j]);
			else addedge(S,id(i,h),len[i]);
		}
		
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			for(int k=1;k<=len[i];++k) {
				int h=1;
				while(k+h-1<=len[i]&&h<=len[j]&&s[i][k+h-1]==s[j][h]) ++h;
				
//				cerr<<i<<" "<<j<<" "<<k<<" "<<h<<endl;
				
				if(k+h-1<=len[i]&&h<=len[j]) continue;
				
				int li=len[i]-k+1,lj=len[j];
				if(li==lj) addedge(id(i,k),T,0);
				else if(li<lj) addedge(id(i,k),id(j,h),lj-li);
				else addedge(id(i,k),id(i,k+h-1),0);
			}
		
	dijkstra();
	if(dis[T]==1e9)cout<<0<<'\n';
	else cout<<dis[T]<<'\n';
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 15620
final_rank: 4

code_snippet: #include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
//#define int long long
//#define inf 0x3f3f3f3f
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
template<typename T>void read(T &x){
	x=0;bool f=0;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
	if(f)x=-x;
}
void write(char x){putchar(x);}
template<typename T>void write(T x){
	if(x<0)putchar('-'),x=-x;
	char stk[14];int cnt=0;
	do stk[++cnt]=x%10+48,x/=10;while(x);
	for(;cnt;)putchar(stk[cnt--]);
}
template<typename T,typename ...Args>void read(T &x,Args &...args){read(x);read(args...);}
template<typename T,typename ...Args>void write(T x,Args ...args){write(x);write(args...);}
template<typename T>T min(T x,T y){return x<y?x:y;}
template<typename T>T max(T x,T y){return x>y?x:y;}
int n;std::string a[100004];
std::map<std::pair<int,int>,int>mp;int tot;
struct node{int to,net,val;}E[1000004];int h[100004],cnt;
void add(int x,int y,int val){E[++cnt]={y,h[x],val};h[x]=cnt;}
int dis[100004];bool vis[100004];
std::priority_queue<std::pair<int,int> >q;
void work(){
	memset(dis,0x3f,sizeof dis);q.push({0,mp[{0,0}]});
	dis[mp[{0,0}]]=0;
	for(;q.size();){
		auto x=q.top();q.pop();
		if(vis[x.second])continue;
		vis[x.second]=1;
		for(int i=h[x.second];i;i=E[i].net){
			int v=E[i].to;
			if(dis[v]>dis[x.second]+E[i].val){
				dis[v]=dis[x.second]+E[i].val;
				q.push({-dis[v],v});
			}
		}
	}
}
signed main(){
	read(n);
	for(int i=1;i<=n;++i)std::cin>>a[i];
	mp[{0,0}]=++tot;
	for(int i=1;i<=n;++i){
		if(!mp[{i,a[i].size()}])mp[{i,a[i].size()}]=++tot;
		add(mp[{0,0}],mp[{i,a[i].size()}],a[i].size());
	}
	for(int i=1;i<=n;++i)for(int j=1;j<=(int)a[i].size();++j){
		if(!mp[{i,j}])mp[{i,j}]=++tot;
		for(int k=1;k<=n;++k)if(i!=k||j!=a[i].size()){
			if((int)a[k].size()<j&&a[i].substr(a[i].size()-j,a[k].size())==a[k]){
				if(!mp[{i,j-a[k].size()}])mp[{i,j-a[k].size()}]=++tot;
				add(mp[{i,j}],mp[{i,j-a[k].size()}],0);
			}
			else if((int)a[k].size()>=j&&a[i].substr(a[i].size()-j)==a[k].substr(0,j)){
				if(!mp[{k,a[k].size()-j}])mp[{k,a[k].size()-j}]=++tot;
				add(mp[{i,j}],mp[{k,a[k].size()-j}],a[k].size()-j);
			}
		}
	}
	work();int ans=1e9;
	for(int i=1;i<=n;++i)ans=min(ans,dis[mp[{i,0}]]);
	write(ans==1e9?0:ans,'\n');
	return 0;
}
accuracy: 1
time_ms: 320
memory_kb: 19792
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<deque>
#include<time.h>
using namespace std;
inline void read(int &x){
	x=0;
	bool sgn=0;
	char ch;
	while(ch=getchar(),ch<'!');
	if(ch=='-'){
		sgn=1;
	}else{
		x=ch-48;
	}
	while(ch=getchar(),ch>'!'){
		x=(x<<3)+(x<<1)+ch-48;
	}
	if(sgn==1){
		x=-x;
	}
	return;
}
inline void write(int x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9){
		write(x/10);
	}
	putchar(x%10+48);
	return;
}
inline void read(long long &x){
	x=0;
	bool sgn=0;
	char ch;
	while(ch=getchar(),ch<'!');
	if(ch=='-'){
		sgn=1;
	}else{
		x=ch-48;
	}
	while(ch=getchar(),ch>'!'){
		x=(x<<3)+(x<<1)+ch-48;
	}
	if(sgn==1){
		x=-x;
	}
	return;
}
inline void write(long long x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9){
		write(x/10);
	}
	putchar(x%10+48);
	return;
}
long long n,dis[17][1<<16];
char x[1050][20];
bool vis[17][1<<16];
struct pair_hash{
	template<class T1,class T2>
	std::size_t operator()(const std::pair<T1, T2>& p)const{
		auto h1=std::hash<T1>{}(p.first);
		auto h2=std::hash<T2>{}(p.second);
		return h1^h2;
	}
};
unordered_set<pair<long long ,long long >,pair_hash>S[17][1<<16];
struct node{
	long long x,y,v;
	node(){}
	node(long long x,long long y,long long v):x(x),y(y),v(v){}
	bool operator<(const node &A)const{
		return v>A.v;
	}
};
vector<node>V[17][1<<16];
void dfs(long long bit,long long val){
	if(bit==0||vis[bit][val]){
		return;
	}
	vis[bit][val]=1;
	for(auto P:S[bit][val]){
		dfs(P.first,P.second);
		V[P.first][P.second].push_back(node(bit,val,bit));
	}
	long long tmp=val,val2=0;
	for(int i=bit-1;i;i--){
		val2<<=1;
		if(val&(1<<i)){
			val^=1<<i,val2|=1;
		}
		if(S[bit-i][val2].count({0,0})){
			dfs(i,val);
			V[i][val].push_back(node(bit,tmp,bit-i));
		}
	}
	return;
}
void bfs(){
	priority_queue<node>Q;
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	Q.push(node(0,0,0));
	dis[0][0]=0;
	while(!Q.empty()){
		node P=Q.top();
		Q.pop();
		if(vis[P.x][P.y]){
			continue;
		}
		vis[P.x][P.y]=1;
		for(auto PP:V[P.x][P.y]){
			long long x=PP.x,y=PP.y,val=PP.v;
			if(vis[x][y]){
				continue;
			}
			if(dis[x][y]>dis[P.x][P.y]+val){
				dis[x][y]=dis[P.x][P.y]+val;
				Q.push(node(x,y,dis[x][y]));
			}
		}
	}
	return;
}
int main(){
	read(n);
	for(int i=1;i<=n;i++){
		scanf("%s",x[i]);
		long long len=strlen(x[i]),val=0;
		for(int j=0;j<len;j++){
			val=(val<<1)|(x[i][j]-'0');
		}
		V[0][0].push_back(node(len,val,len));
		long long val2=0;
		for(int i=len-1;i>=0;i--){
			S[i+1][val].insert({len-i-1,val2});
			if(val&1){
				val2=val2+(1<<(len-1-i));
			}
			val>>=1;
		}
	}
	for(int i=1;i<=n;i++){
		long long len=strlen(x[i]),val=0;
		for(int j=0;j<len;j++){
			val=(val<<1)|(x[i][j]-'0');
		}
		for(auto P:S[len][val]){
			if(P.first==0&&P.second==0){
				continue;
			}
			dfs(P.first,P.second);
		}
	}
	bfs();
	long long ans=2e9;
	for(int i=1;i<=n;i++){
		long long len=strlen(x[i]),val=0;
		for(int j=0;j<len;j++){
			val=(val<<1)|(x[i][j]-'0');
		}
		for(auto P:S[len][val]){
			if(P.first==0&&P.second==0){
				continue;
			}
			ans=min(ans,len+dis[P.first][P.second]);
		}
	}
	if(ans>1e9){
		ans=0;
	}//sbdhbjhdgjhdghd
	write(ans);
	putchar('\n');
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 101416
final_rank: 15

code_snippet: #include<map>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
//#define int long long
//#define inf 0x3f3f3f3f
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
template<typename T>void read(T &x){
	x=0;bool f=0;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
	if(f)x=-x;
}
void write(char x){putchar(x);}
template<typename T>void write(T x){
	if(x<0)putchar('-'),x=-x;
	char stk[24];int cnt=0;
	do stk[++cnt]=x%10+48,x/=10;while(x);
	for(;cnt;)putchar(stk[cnt--]);
}
template<typename T,typename ...Args>void read(T &x,Args &...args){read(x);read(args...);}
template<typename T,typename ...Args>void write(T x,Args ...args){write(x);write(args...);}
template<typename T>T min(T x,T y){return x<y?x:y;}
template<typename T>T max(T x,T y){return x>y?x:y;}
int n;std::string a[100004];
std::map<std::pair<int,int>,int>mp;int tot;
struct node{int to,net,val;}E[1000004];int h[100004],cnt;
void add(int x,int y,int val){E[++cnt]={y,h[x],val};h[x]=cnt;}
int dis[100004];bool vis[100004];
std::priority_queue<std::pair<int,int> >q;
void work(){
	memset(dis,0x3f,sizeof dis);q.push({0,mp[{0,0}]});
	dis[mp[{0,0}]]=0;
	for(;q.size();){
		auto x=q.top();q.pop();
		if(vis[x.second])continue;
		vis[x.second]=1;
		for(int i=h[x.second];i;i=E[i].net){
			int v=E[i].to;
			if(dis[v]>dis[x.second]+E[i].val){
				dis[v]=dis[x.second]+E[i].val;
				q.push({-dis[v],v});
			}
		}
	}
}
signed main(){
	read(n);
	for(int i=1;i<=n;++i)std::cin>>a[i];
	mp[{0,0}]=++tot;
	for(int i=1;i<=n;++i){
		if(!mp[{i,a[i].size()}])mp[{i,a[i].size()}]=++tot;
		add(mp[{0,0}],mp[{i,a[i].size()}],a[i].size());
	}
	for(int i=1;i<=n;++i)for(int j=0;j<=(int)a[i].size();++j){
		if(!mp[{i,j}])mp[{i,j}]=++tot;
		for(int k=1;k<=n;++k)if(i!=k){
			if((int)a[k].size()<j&&a[i].substr(a[i].size()-j,a[k].size())==a[k]){
				if(!mp[{i,j-a[k].size()}])mp[{i,j-a[k].size()}]=++tot;
				add(mp[{i,j}],mp[{i,j-a[k].size()}],0);
			}
			else if(a[i].substr(a[i].size()-j)==a[k].substr(0,j)){
				if(!mp[{k,a[k].size()-j}])mp[{k,a[k].size()-j}]=++tot;
				add(mp[{i,j}],mp[{k,a[k].size()-j}],a[k].size()-j);
			}
		}
	}
	work();int ans=1e9;
	for(int i=1;i<=n;++i)ans=min(ans,dis[mp[{i,0}]]);
	write(ans==1e9?0:ans,'\n');
	return 0;
}
accuracy: 0.12698412698412698
time_ms: 10
memory_kb: 8356
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1010;
int n;
string a[N];
int len[N];
int h(int i,int j){
	return (i-1)*17+j;
}
vector<pair<int,int> >E[N*18];
int vis[N*18],dis[N*18];
signed main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		cin>>a[i];
		len[i]=a[i].size();
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<=len[i];j++){
			int x=len[i]-j;
			if(!j){
				for(int k=1;k<=n;k++){
					// if(k==i)continue;
					E[h(i,j)].push_back({h(k,len[k]),len[k]});
					// cout<<i<<"a "<<j<<"  "<<k<<" "<<len[k]<<"  "<<len[k]<<endl;
				}
			}
			else{
				for(int k=1;k<=n;k++){
					if(k==i&&j==len[i])continue;
					int t=x;
					int y=0;
					while(x<len[i]&&y<len[k]&&a[i][x]==a[k][y]){x++;y++;}
					if(x==len[i]){
						E[h(i,j)].push_back({h(k,len[k]-y),len[k]-j});
						// cout<<i<<"b "<<j<<"  "<<k<<" "<<len[k]-y<<"  "<<len[k]-j<<endl;
					}
					else if(y==len[k]){
						E[h(i,j)].push_back({h(i,j-y),0});
						// cout<<i<<"c "<<j<<"  "<<i<<" "<<j-y<<"  "<<0<<endl;
					}
					x=t;
				}
			}
		}
	}
	memset(dis,127,sizeof(dis));
	memset(vis,0,sizeof(vis));
	priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
	for(int i=1;i<=n;i++){
		dis[h(i,len[i])]=len[i];
		q.push({dis[h(i,len[i])],h(i,len[i])});
	}
	while(!q.empty()){
		int u=q.top().second;q.pop();
		if(vis[u])continue;
		vis[u]=1;
		for(pair<int,int>i:E[u]){
			int v=i.first,w=i.second;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				if(!vis[v]){
					q.push({dis[v],v});
				}
			}
		}
	}
	int ans=1e18;
	for(int i=1;i<=n;i++)ans=min(ans,dis[h(i,0)]);
	if(ans==1e18)ans=0;
	printf("%lld\n",ans);
	return 0;
}
/*
*/
accuracy: 1
time_ms: 80
memory_kb: 41728
final_rank: 11