#include #define rep(i,n) for(int i=0;i<(int)n;i++) #define Rep(i,h,t) for(int i=(int)h;i<(int)t;i++) #define Rrep(i,n) for(int i=(int)n;i>=0;i--) #define RRep(i,h,t) for(int i=(int)h;i>=(int)t;i--) #define UNIQ(v) v.erase(unique(v.begin(),v.end()),v.end()); #define pb push_back #define Fi first #define Se second template bool chmax(T& vmax,const T& v) { if(vmax bool chmin(T& vmin,const T& v) { if(vmin>v) { vmin=v; return true; } return false; } using namespace std; using ll=long long; using lb=long double; using veci=vector; using vec2i=vector>; using vecll=vector; using vec2ll=vector>; using p2i=pair; using p2ll=pair; using m2i=map; using m2ll=map; using msi=map; ll gcd(ll a,ll b) { if(b==0) { return a; } return gcd(b,a%b); } ll lcm(ll a,ll b) { return ll(a/gcd(a,b))*b; } void yn_out(bool b,string yes_out="Yes",string no_out="No") { cout<<(b ? yes_out : no_out)< S,int N,int y){ rep(j,N){ if(S[y][j]!=S[y][2*N-1-j]){ return false; } } return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); //cout<>N>>K; vector S(2*N); rep(i,2*N){ cin>>S[i]; } // W:昇順 B:降順 set> W,B; vec2ll C(2*N,vecll(2*N)); veci check_sym(2*N,0); ll ans=-1e16; ll sum=0; rep(i,2*N){ rep(j,2*N){ cin>>C[i][j]; if(S[i][j]=='.'){ W.insert({-C[i][j],i*2*N+j}); } else{ B.insert({C[i][j],i*2*N+j}); sum+=C[i][j]; } } } int sym_flag=1; rep(i,2*N){ check_sym[i]=is_symmetry(S,N,i); sym_flag&=check_sym[i]; } if(sym_flag){ ans=sum+K; } else{ ans=sum; } while(!W.empty()&&!B.empty()){ auto [w,wp]=*W.begin(); auto [b,bp]=*B.begin(); W.erase(W.begin()); B.erase(B.begin()); w*=-1; if(b>w){ break; } sum+=w-b; W.insert({-b,bp}); B.insert({w,wp}); swap(S[wp/(2*N)][wp%(2*N)],S[bp/(2*N)][bp%(2*N)]); check_sym[wp/(2*N)]=is_symmetry(S,N,wp/(2*N)); check_sym[bp/(2*N)]=is_symmetry(S,N,bp/(2*N)); if(sym_flag){ sym_flag=0; } else{ sym_flag=1; rep(i,2*N){ sym_flag&=check_sym[i]; if(!sym_flag) break; } } if(sym_flag){ chmax(ans,sum+K); } else{ chmax(ans,sum); } } cout<