Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 long long minMoves(vector<int>& balance) {8 const auto& total = accumulate(cbegin(balance), cend(balance), 0LL);9 if (total < 0) {10 return -1;11 }12 13 const auto& cost = [&](int i) {14 long long result = 0, prefix = 0;15 priority_queue<int64_t> max_heap;16 for (int j = 0; j < size(balance); ++j) {17 prefix += balance[(i + j) % size(balance)];18 const auto c = clamp(prefix, 0LL, total);19 result += llabs(prefix - c);20 21 22 max_heap.emplace(c);23 if (max_heap.top() > c) {24 result += max_heap.top() - c;25 max_heap.pop();26 max_heap.emplace(c);27 }28 }29 return result;30 };31 32 long long result = numeric_limits<long long>::max();33 for (int i = 0; i < size(balance); ++i) {34 result = min(result, cost(i));35 }36 return result;37 }38};39 40414243#include <bits/stdc++.h>44 45464748const long long INF = numeric_limits<long long>::max();49 50struct MCMF {51 struct edge {52 int from, to, rev;53 long long cap, cost, flow;54 };55 56 int N;57 vector<vector<edge>> ed;58 vector<int> seen;59 vector<long long> dist, pi;60 vector<edge*> par;61 62 MCMF(int N) : N(N), ed(N), seen(N), dist(N), pi(N), par(N) {}63 64 void addEdge(int from, int to, long long cap, long long cost) {65 if (from == to) return;66 ed[from].push_back({from, to, (int)ed[to].size(), cap, cost, 0});67 ed[to].push_back({to, from, (int)ed[from].size() - 1, 0, -cost, 0});68 }69 70 void path(int s) {71 fill(seen.begin(), seen.end(), 0);72 fill(dist.begin(), dist.end(), INF);73 fill(par.begin(), par.end(), nullptr);74 dist[s] = 0;75 using State = pair<long long, int>;76 priority_queue<State, vector<State>, greater<State>> q;77 q.push({0, s});78 while (!q.empty()) {79 auto [d, u] = q.top();80 q.pop();81 if (d != dist[u]) continue;82 seen[u] = 1;83 for (edge& e : ed[u]) {84 if (e.cap - e.flow <= 0) continue;85 long long val = d + pi[u] - pi[e.to] + e.cost;86 if (val < dist[e.to]) {87 dist[e.to] = val;88 par[e.to] = &e;89 q.push({val, e.to});90 }91 }92 }93 for (int i = 0; i < N; ++i)94 if (dist[i] != INF)95 pi[i] += dist[i];96 }97 98 pair<long long, long long> maxflow(int s, int t) {99 long long totflow = 0, totcost = 0;100 while (path(s), seen[t]) {101 long long fl = INF;102 for (edge* x = par[t]; x; x = par[x->from])103 fl = min(fl, x->cap - x->flow);104 totflow += fl;105 for (edge* x = par[t]; x; x = par[x->from]) {106 x->flow += fl;107 ed[x->to][x->rev].flow -= fl;108 }109 }110 for (int i = 0; i < N; ++i)111 for (edge& e : ed[i])112 totcost += e.cost * e.flow;113 return {totflow, totcost / 2};114 }115};116 117class Solution2 {118public:119 long long minMoves(vector<int>& balance) {120 if (accumulate(cbegin(balance), cend(balance), 0LL) < 0) {121 return -1;122 }123 int S = size(balance), T = size(balance) + 1;124 MCMF mcmf(size(balance) + 2);125 for (int i = 0; i < size(balance); ++i) {126 mcmf.addEdge(i, (i + 1) % size(balance), INF, 1);127 mcmf.addEdge((i + 1) % size(balance), i, INF, 1);128 }129 int64_t demand = 0;130 for (int i = 0; i < size(balance); ++i) {131 if (balance[i] > 0) {132 mcmf.addEdge(S, i, balance[i], 0);133 } else if (balance[i] < 0) {134 mcmf.addEdge(i, T, -balance[i], 0);135 demand += -balance[i];136 }137 }138 const auto& [flow, cost] = mcmf.maxflow(S, T);139 return flow == demand ? cost : -1;140 }141};142