Language: C++

#include <cstdio>
#include <algorithm>
#include <queue>
#include <climits>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#define f first
#define s second
#define mp make_pair

inline int getInt(){ int s; scanf("%d", &s); return s; }

#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)

using namespace std;

struct Edge{
  int cap; // capacity
  int to;
  int rev; // reverse edge id

  Edge(){}
  Edge(int c, int t, int r) :
    cap(c), to(t), rev(r){}
};

struct CostEdge : public Edge{
  int cost;
  CostEdge() : Edge() {}
  CostEdge(int c, int t, int cs, int r) :
    Edge(c, t, r), cost(cs){}
};
  
template<class E> // Edge type
class Graph{
public:
  typedef std::vector<std::vector<E> > G;

private:
  G g;

public:
  Graph(int n) : g(G(n)) {}

  void addEdge(int from, int to, int cap){
    g[from].push_back(E(cap, to, g[to].size()));
    g[to].push_back(E(0, from, g[from].size() - 1));
  }

  void addEdge(int from, int to, int cap, int cost){
    g[from].push_back(E(cap, to, cost, g[to].size()));
    g[to].push_back(E(0, from, -cost, g[from].size() - 1));
  }

  G &getRowGraph(){
    return g;
  }
};

template<class E>
int minCostFlow(Graph<E> &graph, int s, int t, int f, bool negative = false){
  typedef pair<int, int> P;
  typename Graph<E>::G &g = graph.getRowGraph();
  int n = g.size();
  int res = 0;
  vector<int> h(n, 0);
  vector<int> prevv(n);
  vector<int> preve(n);
  const int inf = 10000000;

  if(negative){
    vector<int> dist(n, inf);
    dist[s] = 0;
    
    bool update = true;
    
    while(update){
      update = false;
      for(int v = 0; v < n; v++){
	if(dist[v] == inf) continue;
	for(int i = 0; i < (int)g[v].size(); i++){
	  E &e = g[v][i];
	  if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){
	    dist[e.to]  = dist[v] + e.cost;
	    prevv[e.to] = v;
	    preve[e.to] = i;
	    update      = true;
	  }
	}
      }
    }

    for(int i = 0; i < n; i++)
      h[i] = dist[i];
  }

  while(f > 0){
    priority_queue<P, vector<P>, greater<P> > que;
    vector<int> dist(n, inf);
    dist[s] = 0;
    que.push(P(0, s));

    while(!que.empty()){
      P p   = que.top(); que.pop();
      int v = p.second;
      if(dist[v] < p.first) continue;
      for(int i = 0; i < (int)g[v].size(); i++){
	E &e = g[v][i];

	if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
	  dist[e.to]  = dist[v] + e.cost + h[v] - h[e.to];
	  prevv[e.to] = v;
	  preve[e.to] = i;
	  que.push(P(dist[e.to], e.to));
	}
      }
    if(dist[t] == inf){
      return -1;
    }

    for(int v = 0; v < n; v++) h[v] += dist[v];

    int d = f;
    for(int v = t; v != s; v = prevv[v]){
      d = min(d, g[prevv[v]][preve[v]].cap);
    }

    f   -= d;
    res += d * h[t];

    for(int v = t; v != s; v = prevv[v]){
      E &e = g[prevv[v]][preve[v]];
      e.cap -= d;
      g[v][e.rev].cap += d;
    }
  }
  return res;
}

int main(){
  int m = getInt();
  int n = getInt();
  int k = getInt();

  vector<int> w(n);
  vector<int> a(k);

  Graph<CostEdge> g(k);

  REP(i,n) w[i] = getInt();
  REP(i,k) a[i] = getInt() - 1;

  int ans = 0;
  REP(i,k) ans += w[a[i]];

  vector<int> prevs(n, -1);

  REP(i,k){
    if(i != k - 1) g.addEdge(i, i + 1, 10000, 0);
    int aa = a[i];
    if(prevs[aa] != -1){
      int j = prevs[aa];
      if(j + 1 == i){
	ans -= w[a[i]];
      }else{
	g.addEdge(j + 1, i, 1, - w[a[i]]);
      }
    }
    prevs[aa] = i;
  }

  ans += minCostFlow(g, 0, k - 1, m - 1, true);

  printf("%d\n", ans);

  return 0;
}

Compiler output:
a.cc: In function 'int minCostFlow(Graph<E>&, int, int, int, bool)':
a.cc:140:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  140 | int main(){
      |         ^~
a.cc:140:9: note: remove parentheses to default-initialize a variable
  140 | int main(){
      |         ^~
      |         --
a.cc:140:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:140:11: error: a function-definition is not allowed here before '{' token
  140 | int main(){
      |           ^
a.cc:177:2: error: expected '}' at end of input
  177 | }
      |  ^
a.cc:61:77: note: to match this '{'
   61 | int minCostFlow(Graph<E> &graph, int s, int t, int f, bool negative = false){
      |                                                                             ^