首页 技术 正文
技术 2022年11月13日
0 收藏 625 点赞 2,514 浏览 2529 个字

A string a of length m is called antipalindromic iff m is even, and for each i (1 ≤ i ≤ mai ≠ am - i + 1.

Ivan has a string s consisting of n lowercase Latin letters; n is even. He wants to form some string t that will be an antipalindromic permutation of s. Also Ivan has denoted the beauty of index i as bi, and the beauty of t as the sum of bi among all indices i such that si = ti.

Help Ivan to determine maximum possible beauty of t he can get.

Input

The first line contains one integer n (2 ≤ n ≤ 100, n is even) — the number of characters in s.

The second line contains the string s itself. It consists of only lowercase Latin letters, and it is guaranteed that its letters can be reordered to form an antipalindromic string.

The third line contains n integer numbers b1b2, …, bn (1 ≤ bi ≤ 100), where bi is the beauty of index i.

Output

Print one number — the maximum possible beauty of t.

Examples

Input

8
abacabac
1 1 1 1 1 1 1 1

Output

8

Input

8
abaccaba
1 2 3 4 5 6 7 8

Output

26

Input

8
abacabca
1 2 3 4 4 3 2 1

Output

17

题意:给定长长度为N的字符串,现在求一个重排列,使得对称位置不相同。如果重排后i位置的字母和原来相同,就得到对应位置的得分,求最大得分。

保证N是偶数,保证有满足条件的重排。

思路:最大费用最大流。

建图:

S–>26个字母:(字母个数,0);

字母–>N/2个位置:(1,val);val尽可能大就行,三种情况讨论。

N/2个位置–>T:(2,0)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=<<;int To[maxn],Laxt[maxn],Next[maxn],cap[maxn],cost[maxn];
int S,T,cnt=,dis[maxn],ans;
bool inq[maxn],vis[maxn];
deque<int>q;
void add(int u,int v,int c,int cc)
{
Next[++cnt]=Laxt[u];Laxt[u]=cnt; To[cnt]=v;cap[cnt]=c;cost[cnt]=-cc;
Next[++cnt]=Laxt[v];Laxt[v]=cnt; To[cnt]=u;cap[cnt]=;cost[cnt]=cc;
}
bool spfa()
{
for(int i=;i<=T;i++) inq[i]=;
for(int i=;i<=T;i++) dis[i]=inf;
inq[T]=; dis[T]=; q.push_back(T);
while(!q.empty())
{
int u=q.front(); q.pop_front();
inq[u]=;
for(int i=Laxt[u];i;i=Next[i])
{
int v=To[i];
if(cap[i^]&&dis[v]>dis[u]-cost[i])
{
dis[v]=dis[u]-cost[i];
if(!inq[u]){
inq[v]=;
if(q.empty()||dis[v]>dis[q.front()]) q.push_back(v);
else q.push_front(v);
}
}
}
}
return dis[S]<inf;
}
int dfs(int u,int flow)
{
vis[u]=;
if(u==T||flow==) return flow;
int tmp,delta=;
for(int i=Laxt[u];i;i=Next[i])
{
int v=To[i];
if((!vis[v])&&cap[i]&&dis[v]==dis[u]-cost[i])
{
tmp=dfs(v,min(cap[i],flow-delta));
delta+=tmp; cap[i]-=tmp; cap[i^]+=tmp;
}
}
return delta;
}
char c[maxn]; int v[maxn],num[maxn];
int main()
{
int N,i,j;
scanf("%d",&N); scanf("%s",c+);
for(i=;i<=N;i++) scanf("%d",&v[i]),num[c[i]-'a'+]++;
S=; T=+N/+;
for(i=;i<=;i++) add(S,i,num[i],);
for(i=;i<=;i++)
for(j=;j<=N/;j++){
if(c[j]-'a'+==i&&c[N+-j]-'a'+==i) add(i,j+,,max(v[j],v[N+-j]));
else if(c[j]-'a'+==i) add(i,j+,,v[j]);
else if(c[N+-j]-'a'+==i) add(i,j+,,v[N+-j]);
else add(i,j+,,);
}
for(i=;i<=N/;i++) add(i+,T,,);
while(spfa()){
vis[T]=;
while(vis[T]){
for(i=;i<=T;i++) vis[i]=;
ans-=dis[S]*dfs(S,N);
}
}
printf("%d\n",ans);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297