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, to, 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, to, -cost, g[from].size() - 1));
  }

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

template<class E>
int minCostFlow(Graph<E> &graph, int s, int t, int f){
  typename Graph<E>::G &g = graph.getRowGraph();
  int n = g.size();
  int res = 0;
  vector<int> prevv(n);
  vector<int> preve(n);
  const int inf = 200000000;
  
  while(f > 0){
    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;
	  }
	}
      }
    }
    
      return -1;
    }
    
    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 * dist[t];
    // printf("f:%d res:%d\n", f, res);
    
    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]];
  // printf("%d\n", ans);

  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]]);
	// printf("%d => %d : %d\n", j + 2, i + 1, - w[aa]);
      }
    }
    prevs[aa] = i;
  }

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

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

  return 0;
}

Compiler output:
a.cc: In function 'int minCostFlow(Graph<E>&, int, int, int)':
a.cc:100:16: error: 'dist' was not declared in this scope
  100 |     res += d * dist[t];
      |                ^~~~
a.cc: At global scope:
a.cc:109:3: error: expected unqualified-id before 'return'
  109 |   return res;
      |   ^~~~~~
a.cc:110:1: error: expected declaration before '}' token
  110 | }
      | ^
a.cc: In function 'int minCostFlow(Graph<E>&, int, int, int) [with E = CostEdge]':
a.cc:108:3: warning: control reaches end of non-void function [-Wreturn-type]
  108 |   }
      |   ^