Language: C++

#include<bits/stdc++.h>
#define re register
using namespace std;
int n,val[52],vis[52],lena,lenb,len[52];
char s[52][52],t[52][52],a[1000002],b[1000002];
long long ans,sum;
inline int read(){
	re int t=0,f=0;re char v=getchar();
	while(v<'0')f|=(v=='-'),v=getchar();
	while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
	return f?-t:t;
}
inline void dfs(){
	if(sum>ans)return;
	if(lena&&lenb&&abs(lena-lenb)<=1){
		ans=sum;
		return;
	}
	if(lena<=lenb){
		for(re int i=1;i<=n;++i){
			re bool ia=1;
			for(re int j=lena+1;j<=min(lenb,lena+len[i]);++j){
			if(b[j]!=s[i][j-lena]){
				ia=0;
				break;
			}}
			if(ia){
				if(vis[i]>20)continue;
				++vis[i];
				re int pp=lena;sum+=val[i];
				for(re int j=lena+1;j<=lena+len[i];++j)a[j]=s[i][j-lena];
				lena+=len[i];
				dfs();
				lena=pp;
				--vis[i];sum-=val[i];
			}
		}
	}
	else{
		for(re int i=1;i<=n;++i){
			re bool ia=1;
			for(re int j=lenb+1;j<=min(lena,lenb+len[i]);++j){
			if(a[j]!=t[i][j-lenb]){
				ia=0;
				break;
			}}
			if(ia){
				if(vis[i]>20)continue;
				++vis[i];
				re int pp=lenb;sum+=val[i];
				for(re int j=lenb+1;j<=lenb+len[i];++j)b[j]=t[i][j-lenb];
				lenb+=len[i];
				dfs();
				lenb=pp;
				--vis[i];sum-=val[i];
			}
		}
	}
}
signed main(){
	ans=1e18;
	n=read();
	for(re int i=1;i<=n;++i){
		scanf("%s",s[i]+1);
		re bool ia=1;
		len[i]=strlen(s[i]+1);
		for(re int j=1;j<=len[i];++j){
		t[i][j]=s[i][len[i]-j+1];if(t[i][j]!=s[i][j]){
			ia=0;
			break;
		}}
		val[i]=read();
		if(ia)ans=min(ans,val[i]);
	}
	dfs();
	if(ans<1e18)printf("%lld\n",ans);
	else puts("-1");
}


Compiler output:
a.cc: In function 'int read()':
a.cc:8:16: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    8 |         re int t=0,f=0;re char v=getchar();
      |                ^
a.cc:8:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    8 |         re int t=0,f=0;re char v=getchar();
      |                    ^
a.cc:8:32: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
    8 |         re int t=0,f=0;re char v=getchar();
      |                                ^
a.cc: In function 'void dfs()':
a.cc:20:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   20 |                 for(re int i=1;i<=n;++i){
      |                            ^
a.cc:21:33: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |                         re bool ia=1;
      |                                 ^~
a.cc:22:36: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   22 |                         for(re int j=lena+1;j<=min(lenb,lena+len[i]);++j){
      |                                    ^
a.cc:30:40: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   30 |                                 re int pp=lena;sum+=val[i];
      |                                        ^~
a.cc:31:44: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   31 |                                 for(re int j=lena+1;j<=lena+len[i];++j)a[j]=s[i][j-lena];
      |                                            ^
a.cc:40:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |                 for(re int i=1;i<=n;++i){
      |                            ^
a.cc:41:33: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   41 |                         re bool ia=1;
      |                                 ^~
a.cc:42:36: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   42 |                         for(re int j=lenb+1;j<=min(lena,lenb+len[i]);++j){
      |                                    ^
a.cc:50:40: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   50 |                                 re int pp=lenb;sum+=val[i];
      |                                        ^~
a.cc:51:44: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   51 |                                 for(re int j=lenb+1;j<=lenb+len[i];++j)b[j]=t[i][j-lenb];
      |                                            ^
a.cc: In function 'int main()':
a.cc:63:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   63 |         for(re int i=1;i<=n;++i){
      |                    ^
a.cc:65:25: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   65 |                 re bool ia=1;
      |                         ^~
a.cc:67:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   67 |                 for(re int j=1;j<=len[i];++j){
      |                            ^
a.cc:73:30: error: no matching function for call to 'min(long long int&, int&)'
   73 |                 if(ia)ans=min(ans,val[i]);
      |                           ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:73:30: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   73 |                 if(ia)ans=min(ans,val[i]);
      |                           ~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:73:30: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   73 |                 if(ia)ans=min(ans,val[i]);
      |                           ~~~^~~~~~~~~~~~