1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <bits/stdc++.h> #define all(vec) vec.begin(),vec.end() #define pb push_back #define EB emplace_back #define EM emplace #define FI first #define SE second #define SZ(a) ((int) a.size()) #define FOR(i, a, b) for (long long i = (a); i <= (b); ++i) #define ROF(i, a, b) for (long long i = (a); i >= (b); --i) #define debug(var) cerr << #var <<":"<<var<<"\n"; #define lson(var) (var<<1) #define rson(var) ((var<<1)+1)
using namespace std;
using ll = long long;using ull = unsigned long long; using DB=double;using LD=long double;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>; using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>; using tll = tuple<ll,ll,ll>; using vpll = vector<pll>; using arr3 = array<ll,3> ;using arr2 = array<ll,2>; constexpr ll MAXN=(ll)3e3+10,INF=(ll)1e17+9; constexpr ll mod=(ll)1e9+7; constexpr double eps=1e-8;const double pi=acos(-1.0); ll N,M,Q,X,K,T,lT,W[MAXN]; char S[MAXN];
inline void __(){ cin>>N; FOR(i,1,N){ cin>>S[i]; } FOR(i,1,17){ cin>>W[i]; } ll len;cin>>len; if(N==1){ cout<<0<<"\n"; return; } vll bits; if(len%2==1){ len+=1; } if(len>N){ cout<<0<<"\n"; return; } FOR(i,1,N){ bitset<20> bi; bool ok=false; FOR(j,1,N){ ll x=i-j+1,y=i+j; if(x<1 || y>N) break; #ifdef LOCAL debug(x);debug(y); #endif if(S[x]!=S[y]){ bi[min(S[x],S[y])-'a']=true; }else{ ok=true; } if(y-x+1==len){ if(ok) continue; bits.pb(bi.to_ullong()); } } } sort(all(bits));bits.resize(unique(all(bits))-bits.begin()); ll ans=INF; #ifdef LOCAL for(auto &b:bits){ bitset<20> bi(b); FOR(i,0,17){ cerr<<bi[i]; } cerr<<"\n"; } FOR(i,0,17){ cerr<<char('a'+i); } cerr<<"\n"; FOR(i,1,17){ cerr<<W[i]; } cerr<<"\n"; #endif FOR(s,0,(1<<17)-1){ bitset<20> bi(s); ll lans=0; FOR(j,0,18){ if(bi[j]){ lans+=W[j+1]; } } if(lans>=ans) continue; bool ok=true; for(auto &b:bits){ if(b==0) continue; if((s&b)==0){ ok=false; break; } } if(ok){ ans=lans; } } cout<<ans<<"\n"; }
int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>lT; while(lT--) __(); return 0; }
|