Language: C++

int N;
int a[128];
bool flag[128];

#define MAX_N 128
#define INF (2<<21)

class dinic {
private:
	struct edge { int to, cap, rev; };
	vector<edge> G[MAX_N];
	//sからの深さを記憶
	vector<int> level;
	int V;

	//深さ優先で経路を検索
	//経路を見つけたら流して更新
	int dfs(int v, int t, int f) {
		if (v == t) return f;
		for (int i = 0; i < G[v].size(); i++) {
			edge &e = G[v][i];
			if (e.cap <= 0) continue;
			if (level[v] >= level[e.to]) continue;
			int d = dfs(e.to, t, min(f, e.cap));
			if (d > 0) {
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
		return 0;
	}

	//幅優先
	void bfs(int s) {
		level.assign(V, -1);
		queue<int> q;
		q.push(s);
		level[s] = 0;
		for (; !q.empty();) {
			int v = q.front(); q.pop();
			for (int i = 0; i < G[v].size(); i++) {
				struct edge e = G[v][i];
				if (e.cap <= 0) continue;
				//既に通った辺なら
				if (level[e.to] >= 0) continue;
				level[e.to] = level[v] + 1;
				q.push(e.to);
			}
		}
	}


public:
	dinic(int v) {
		V = v;
	}
	void add_edge(int from, int to, int cap) {
		struct edge tmp;
		tmp = { to, cap, int(G[to].size()) };
		G[from].push_back(tmp);
		tmp = { from, 0, int(G[from].size() - 1) };
		G[to].push_back(tmp);
	}

	int run(int s, int t) {
		int f = 0;
		for (;;) {
			bfs(s);
			if (level[t] < 0) break;
			f += dfs(s, t, INF);
		}
		return f;
	}
};

void MUL() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> a[i];
	}
	//後半で負の宝石は問答無用で叩き割る
	for (int i = (N + 1 / 2); i < N; i++) {
		a[i] = a[i] < 0 ? 0 : a[i];
	}

	int ideal = 0;
	for (int i = 0; i < N; i++) {
		ideal += a[i] > 0 ? a[i] : 0;
	}

	memset(flag, 0, sizeof(flag));

	dinic d(N + 2);
	for (int i = 0; i < N; i++) {
		if (a[i] > 0) {
			d.add_edge(0, i + 1, a[i]);
			d.add_edge(i + 1, N + 1, 0);
		}
		else {
			d.add_edge(i + 1, N + 1, -a[i]);
			d.add_edge(0, i + 1, 0);
		}
		for (int j = (i + 1) * 2; j < N + 1; j += i + 1) {
			d.add_edge(j, (i + 1), INF);
		}
	}
	cout << ideal - d.run(0, N + 1) << endl;
}

int main() {
	MUL();
	return 0;
}


Compiler output:
a.cc:11:9: error: 'vector' does not name a type
   11 |         vector<edge> G[MAX_N];
      |         ^~~~~~
a.cc:13:9: error: 'vector' does not name a type
   13 |         vector<int> level;
      |         ^~~~~~
a.cc: In member function 'int dinic::dfs(int, int, int)':
a.cc:20:37: error: 'G' was not declared in this scope
   20 |                 for (int i = 0; i < G[v].size(); i++) {
      |                                     ^
a.cc:23:29: error: 'level' was not declared in this scope
   23 |                         if (level[v] >= level[e.to]) continue;
      |                             ^~~~~
a.cc:24:46: error: 'min' was not declared in this scope
   24 |                         int d = dfs(e.to, t, min(f, e.cap));
      |                                              ^~~
a.cc: In member function 'void dinic::bfs(int)':
a.cc:36:17: error: 'level' was not declared in this scope
   36 |                 level.assign(V, -1);
      |                 ^~~~~
a.cc:37:17: error: 'queue' was not declared in this scope
   37 |                 queue<int> q;
      |                 ^~~~~
a.cc:37:23: error: expected primary-expression before 'int'
   37 |                 queue<int> q;
      |                       ^~~
a.cc:38:17: error: 'q' was not declared in this scope
   38 |                 q.push(s);
      |                 ^
a.cc:42:45: error: 'G' was not declared in this scope
   42 |                         for (int i = 0; i < G[v].size(); i++) {
      |                                             ^
a.cc: In member function 'void dinic::add_edge(int, int, int)':
a.cc:60:38: error: 'G' was not declared in this scope
   60 |                 tmp = { to, cap, int(G[to].size()) };
      |                                      ^
a.cc:60:52: error: no match for 'operator=' (operand types are 'dinic::edge' and '<brace-enclosed initializer list>')
   60 |                 tmp = { to, cap, int(G[to].size()) };
      |                                                    ^
a.cc:10:16: note: candidate: 'constexpr dinic::edge& dinic::edge::operator=(const dinic::edge&)'
   10 |         struct edge { int to, cap, rev; };
      |                ^~~~
a.cc:10:16: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const dinic::edge&'
a.cc:10:16: note: candidate: 'constexpr dinic::edge& dinic::edge::operator=(dinic::edge&&)'
a.cc:10:16: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'dinic::edge&&'
a.cc:62:58: error: no match for 'operator=' (operand types are 'dinic::edge' and '<brace-enclosed initializer list>')
   62 |                 tmp = { from, 0, int(G[from].size() - 1) };
      |                                                          ^
a.cc:10:16: note: candidate: 'constexpr dinic::edge& dinic::edge::operator=(const dinic::edge&)'
   10 |         struct edge { int to, cap, rev; };
      |                ^~~~
a.cc:10:16: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const dinic::edge&'
a.cc:10:16: note: candidate: 'constexpr dinic::edge& dinic::edge::operator=(dinic::edge&&)'
a.cc:10:16: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'dinic::edge&&'
a.cc: In member function 'int dinic::run(int, int)':
a.cc:70:29: error: 'level' was not declared in this scope
   70 |                         if (level[t] < 0) break;
      |                             ^~~~~
a.cc: In function 'void MUL()':
a.cc:78:9: error: 'cin' was not declared in this scope
   78 |         cin >> N;
      |         ^~~
a.cc:92:9: error: 'memset' was not declared in this scope
   92 |         memset(flag, 0, sizeof(flag));
      |         ^~~~~~
a.cc:1:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
  +++ |+#include <cstring>
    1 | int N;
a.cc:108:9: error: 'cout' was not declared in this scope
  108 |         cout << ideal - d.run(0, N + 1) << endl;
      |         ^~~~
a.cc:108:44: error: 'endl' was not declared in this scope
  108 |         cout << ideal - d.run(0, N + 1) << endl;
      |                                            ^~~~