--> Set personal I to the person on the left of cions: --------------> x1 = x1 + 0; 1st: a [1]-x1 + x2 = M; --> x2 = x1-(a [1]-M) 2nd People: a [2]-x2 + x3 = M; --> x3 = x1-(a [1]-M)-(a [2]-M )...... N-1 person: a [n-1]-x (n-1) + xn = M; --> xn = x1-(a [1]-M) -(a [2]-M )-... -(a [n-1]-M); nth person: a [n]-xn + x1 = M; (this is useless) if c [I] = c [I-1] + a [I]-M; then there are: --> x1 = x1 + c [0] --> x2 = x1-c [1] --> x3 = x1-c [2]… --> X (n-1) = x1-c [n]; the requirement is | x1 | + | x1 + c [0] | + | x1-c [1] | +... + | x1-c [n] | minimum value --> median! [Cpp] # include <iostream> # include <algorithm> # include <cmath> using namespace std; const int maxn = 1000001 + 10; long a [maxn], c [maxn]; // a [I] is the initial capital of the input I-th individual, c [I] is a [1]-M + a [2]-M +... + a [I]-M int main () {int n, I; while (cin> n) {long sum = 0; // sum for (I = 1; I <= n; I ++) {cin> a [I]; sum + = a [I];} long M = sum/n; // calculate the final money for each user. c [0] = 0; for (I = 1; I <n; I ++) c [I] = c [I-1] + a [I]-M; sort (c, c + n); // sort from small to large, in order to obtain the median long x1 = c [n/2]; // The subscript sum = 0 for the median; // sum is used to calculate the total number of coins exchanged for (I = 0; I <n; I ++) sum + = abs (x1-c [I]); cout <sum <endl;} return 0 ;}