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
|
#include<bits/stdc++.h> #define re register const int MAXM=12,MAXN=1e4+10,MAXK=1e4+10,MAXV=1e4+10,MAXE=1e5+10; const int INF=0x3f3f3f3f; int M,N,K,S,T; int Wgt[MAXN]; struct Net { int next,to,val,cost; }Edge[MAXE<<1]; int Head[MAXV],Cur[MAXV],Total=1; inline void addEdge(int u,int v,int w,int c) { Edge[++Total]=(Net){Head[u],v,w,c};Head[u]=Total; Edge[++Total]=(Net){Head[v],u,0,-c};Head[v]=Total; } int Dist[MAXV],ret; bool Vis[MAXV]; inline bool Spfa(int s,int t) { for(int i=S;i<=T;++i) Dist[i]=INF; for(int i=S;i<=T;++i) Cur[i]=Head[i]; std::queue<int>Q; Q.push(s),Dist[s]=0,Vis[s]=1; while(!Q.empty()) { int u=Q.front();Q.pop(); Vis[u]=0; for(int e=Head[u];e;e=Edge[e].next) { int v=Edge[e].to; if(Edge[e].val&&Dist[v]>Dist[u]+Edge[e].cost) { Dist[v]=Dist[u]+Edge[e].cost; if(!Vis[v]) Q.push(v),Vis[v]=1; } } } return Dist[t]!=INF; } int Dfs(int x,int inf) { if(x==T) return inf; Vis[x]=1; int flow=0; for(int e=Cur[x],v;e&&flow<inf;e=Edge[e].next) { Cur[x]=e,v=Edge[e].to; if(!Vis[v]&&Edge[e].val&&Dist[v]==Dist[x]+Edge[e].cost) { int k=Dfs(v,std::min(Edge[e].val,inf-flow)); if(k) { ret+=k*Edge[e].cost; Edge[e].val-=k,Edge[e^1].val+=k,flow+=k; } } } Vis[x]=0; return flow; } inline int SSP() { int res=0,flow; while(Spfa(S,T)) while(flow=Dfs(S,INF)) res+=flow; return res; } int Sum,Lst[MAXN],Id[MAXN],Idx; signed main() { scanf("%d%d%d",&M,&N,&K);S=0; for(int i=1;i<=N;++i) scanf("%d",&Wgt[i]); std::memset(Lst,-1,sizeof(Lst)); for(int i=1,x;i<=K;++i) { scanf("%d",&x); if(Id[Idx]!=x) Id[++Idx]=x; } K=Idx,T=Idx+1; for(int i=1;i<=K;++i) { if(~Lst[Id[i]]) addEdge(Lst[Id[i]]+1,i,1,-Wgt[Id[i]]); Lst[Id[i]]=i,Sum+=Wgt[Id[i]]; } addEdge(S,1,M-1,0); for(int i=1;i<=K;++i) addEdge(i,i+1,INF,0); SSP(); printf("%d\n",Sum+ret); return 0; }
|