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){
  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;
	  }
	}
    }
    
    if(dist[t] == inf){
      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];
    
    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);

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

  return 0;
}

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