Description
vjudge 上有中文题面。
Solution
挺好的费用流。
建图太难描述了...我又懒得画图...看代码吧...
#include<bits/stdc++.h>
using namespace std;
template <class T> void read(T &x) {
x = 0; bool flag = 0; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) flag |= ch == '-';
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48; flag ? x = ~x + 1 : 0;
}
#define N 1500
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define INF 0x3f3f3f3f
int S, T, flow, cost, head[N], tot = 1, dis[N], pre[N];
struct edge { int u, v, c, w, next; }e[200001];
queue<int> q;
bool inq[N];
inline void insert(int u, int v, int c, int w) { e[++tot].u = u, e[tot].v = v, e[tot].c = c, e[tot].w = w, e[tot].next = head[u], head[u] = tot; }
inline void add(int u, int v, int c, int w) { insert(u, v, c, w), insert(v, u, 0, -w); }
bool spfa() {
rep(i, S, T) dis[i] = INF; dis[S] = 0; q.push(S);
while (!q.empty()) {
int u = q.front(); q.pop(); inq[u] = 0;
for (int i = head[u], v, w; i; i = e[i].next) if (e[i].c > 0 && dis[v = e[i].v] > dis[u] + (w = e[i].w)) {
dis[v] = dis[u] + w, pre[v] = i;
if (!inq[v]) q.push(v); inq[v] = 1;
}
}
return dis[T] != INF;
}
void mcf() {
int d = INF;
for (int i = T; (i ^ S); i = e[pre[i]].u) d = min(d, e[pre[i]].c);
flow += d;
for (int i = T; (i ^ S); i = e[pre[i]].u) e[pre[i]].c -= d, e[pre[i] ^ 1].c += d, cost += d * e[pre[i]].w;
}
char s[N];
int b[N], cnt[27];
int main() {
int n; read(n);
scanf("%s", s + 1);
rep(i, 1, n) read(b[i]);
rep(i, 1, n) cnt[s[i] - 'a' + 1]++;
T = 26 + n + 26 * n / 2 + 1;
rep(i, 1, 26) {
add(S, i, cnt[i], 0);
rep(j, 1, n / 2) {
int t = 26 + n + (i - 1) * n / 2 + j, d1 = j, d2 = n - j + 1;
add(i, t, 1, 0);
add(t, d1 + 26, 1, -b[d1] * (s[d1] - 'a' + 1 == i));
add(t, d2 + 26, 1, -b[d2] * (s[d2] - 'a' + 1 == i));
}
}
rep(i, 1, n) add(i + 26, T, 1, 0);
while (spfa()) mcf();
printf("%d", -cost);
return 0;
}