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 ;
}

CodeForces - 884F :Anti-Palindromize(贪心&费用流)的更多相关文章

  1. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  2. UVALive 4863 Balloons 贪心/费用流

    There will be several test cases in the input. Each test case will begin with a line with three inte ...

  3. luogu P5470 [NOI2019]序列 dp 贪心 费用流 模拟费用流

    LINK:序列 考虑前20分 容易想到爆搜. 考虑dp 容易设\(f_{i,j,k,l}\)表示前i个位置 选了j对 且此时A选择了k个 B选择了l个的最大值.期望得分28. code //#incl ...

  4. Codeforces 362E Petya and Pipes 费用流建图

    题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...

  5. 【bzoj2424】[HAOI2010]订货 费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...

  6. Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]

    洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...

  7. CodeForces 164C Machine Programming 费用流

    Machine Programming 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co One remark ...

  8. Codeforces Gym 100002 E "Evacuation Plan" 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  9. [SDOI2016]数字配对(费用流+贪心+trick)

    重点是如何找到可以配对的\(a[i]\)和\(a[j]\). 把\(a[i]\)分解质因数.设\(a[i]\)分解出的质因数的数量为\(cnt[i]\). 设\(a[i]\geq a[j]\) 那么\ ...

随机推荐

  1. 鼠标点击input框后里面的内容就消失

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 深入解析Windows窗体创建和消息分发

    Windows GUI採用基于事件驱动的编程模型,其实差点儿全部的界面库都是这样做的.在纯粹的Window32 SDK编程时代.人们还能够搞懂整个Windows窗口创建和消息的流通过程.可是在如今各种 ...

  3. Lua学习九----------Lua字符串

    © 版权声明:本文为博主原创文章,转载请注明出处 1.Lua字符串 - ''单引号间的一串字符 - ""双引号之间的一串字符 - [[]]之间的一串字符 2.Lua转义字符 3.字 ...

  4. Pyqt4 360界面风格皮肤实现

    前言 最近用Pyqt做了软件界面,始终觉得windows风格不太好看,虽然数字公司的行为有争议,但是也不影响我欣赏360卫士的界面风格. 声明 首先声明,此项工作并非原创,而是基于这位zhuyeqin ...

  5. javascript关闭弹出窗体时刷新父窗体和居中显示弹出窗

    居中显示用到了moveTO()方法: 关闭弹出窗时刷新父窗体用到了window.opener方法: 父窗体代码例如以下: <%@ Page Language="C#" Aut ...

  6. JS 的引用赋值与传值赋值

    这个问题说大不大说小不小,如果你有幸踩了这个坑,一定会找这篇文章,哈哈~ 现说一下JS数字的类型:基本类型和引用类型 先看下下面两个栗子: var a = 30; var b = a; a = 20; ...

  7. liunx 安装工具总结

    1  下载相关文件,比如hadoop 2  解压文件 tar -zxcf xxx.tar.gz 3  mv xxx 到指定目录:通常安装到/usr/local 或者自己建个目录 /usr/develo ...

  8. Javascript模块化编程-require.js[3]

    很多情况下,JS都是放到一个或者多个文件里,只要加载这些文件就可以了. 但是对于一些小型项目而言,这种写法是没有任何问题的. 但是对于某些大型网站,JS的量是很大的,如果还采用这种方式,网站时常在加载 ...

  9. ubuntu 下解决sublime v3 中文输入法时 退格键删除不了拼音的问题

    ubuntu下,sulime想要支持中文需要这样设置: 1.安装中文输入解决的github git clone https://github.com/lyfeyaj/sublime-text-imfi ...

  10. Django框架ORM常用字段汇总_模型层

    与数据类型相关的字段 CharField 作用:字符串字段, 用于较短的字符串. 参数:CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许 ...