We need to understand the problem: N persons standing in a row from front to back? It says "from the front" so index 1 is frontmost, N is backmost. Condition: For each person i, "Nobody in front of the person is taller than the person." That is, for each i, for all j < i, (A_j + stool_j) <= (A_i + stool_i). In other words, the sequence of total heights (original height plus stool) must be non-decreasing from front to back? Actually condition: "nobody in front of the person is taller than the person." Means that each person must be at least as tall as all persons in front (i.e., persons with smaller index). So the total height of a person must be >= max of total heights of all persons in front. This implies that the total heights must be a non-decreasing sequence (since if we enforce for each i: total_i >= max_{j < i} total_j, then total_i is at least the previous maximum, so the sequence of maxima is non-decreasing, but total_i may be less than total_{i-1}? Let's test: Suppose total_1 = 5, total_2 = 4. Then for i=2, max_{j<2} total_j = total_1 =5, condition requires total_2 >=5, but total_2=4 fails. So indeed total heights must be non-decreasing (monotonic increasing? non-decreasing because equal is allowed). So we need to assign stool_i >= 0 to each i such that B_i = A_i + stool_i is a non-decreasing sequence, and we minimize sum of stool_i.
This is a classic problem: given an array A, we want to create a non-decreasing array B such that B_i >= A_i and minimize sum(B_i - A_i). The solution: Let current_max = 0? Actually we can process from left to right, and set B_i = max(A_i, B_{i-1}) (with B_1 = A_1). Then sum of differences gives minimum total increments. Because for non-decreasing, the optimal is to lift each element only as needed to not drop below previous B. This is standard: to minimize sum of increments while making sequence non-decreasing, we set each element to max(itself, previous). It's like we want the minimal pointwise increases to achieve monotonicity.
But is there any nuance? The stools heights are non-negative, and we can set B_i >= A_i. The condition requires B_i >= max_{j < i} B_j, equivalently B_i must be at least the maximum of all previous B_j. So the minimal B_i is max(A_i, max_{j < i} B_j). Starting with B_1 = A_1. Then total stools sum = Σ (B_i - A_i). That yields minimal sum. So answer = Σ_{i=1..N} (max(0, current_max - A_i)), where current_max starts as A_1, then updated to max(current_max, A_i). Indeed.
Second example: all 3's: cur=3, each diff=0, total=0. matches.
So answer is simple. But confirm constraints N up to 2e5, A up to 1e9, sum may be large up to N*1e9, need 64-bit.
Implementation: read N, array, iterate, maintain max_sofar, add to ans max(0, max_sofar - A_i), update max_sofar = max(max_sofar, A_i). Print ans.
Edge cases: N=1 => ans=0.
Thus solution is trivial.
But wait, the problem statement says: "Nobody in front of the person is taller than the person." That is B_i >= all B_j for j < i. That's exactly non-decreasing from front to back? Actually, "taller" means strictly taller? Usually "taller than" might be strictly taller. But the condition: "Nobody in front of the person is taller than the person." Means for all j < i, B_j <= B_i (not strictly). If equal, then nobody is taller, they are equal or shorter. So it's non-decreasing. So correct.
Thus answer is simple.
But let's double-check if there is any twist: The stools are added to each person individually. The stools can be any non-negative real? But integer heights, so integer stools, but our method gives integer differences.
Hence answer.
So final code:
int main(){
int N;
cin>>N;
vector A(N);
for(int i=0;i<N;i++) cin>>A[i];
long long cur = A[0];
long long ans = 0;
for(int i=1;i<N;i++){
if(A[i] < cur){
ans += cur - A[i];
} else {
cur = A[i];
}
}
cout << ans << endl;
return 0;
}
Make sure to use long long for cur and ans.
But initial cur = A[0]? Actually we start from i=1 to N-1. But need to include i=0? diff for i=0 is 0. So correct.
Thus solution.
But wait: Some might think it's like we need to adjust each person to be at least as tall as the tallest in front. The obvious approach is to set each person's total height equal to the maximum original height seen so far, but that would cause unnecessary stools for persons who are already taller? Actually if we set B_i = max_{j <= i} A_j (without stools), that would satisfy condition because each B_i is the current maximum; then stools needed are max_{j<=i}A_j - A_i. But that sum is exactly our algorithm? Let's see: For i from 1 to N, define M_i = max(A_1,...,A_i). Then B_i = M_i works, but is it minimal? Possibly not: Consider A = [5,1,6]. M_1=5, B_1=5; M_2=5, B_2=5 (stool 4); M_3=6, B_3=6 (stool 0). But is there a solution with less total stools? Could we set B_1=5, B_2= something like 5? Yes that's needed because we need B_2 >=5. Could we set B_1 lower? B_1 must be at least A_1=5, can't be lower. So B_1=5. B_2 must >=5, so B_2>=5, minimal is 5. B_3 must >= max(B_1,B_2)=5, but also B_3>=A_3=6, so minimal is 6. So indeed minimal. So sum of (M_i - A_i) is minimal. This matches.
Thus answer.
Now, I'll produce final explanation.
But wait, there is a nuance: The condition says: "Nobody in front of the person is taller than the person." That includes all persons in front, not just the immediate front. So indeed monotonic non-decreasing.
Thus final solution is straightforward.
But note: Sometimes a problem might ask for the sum of stools where stools can be of any height, and the condition is for each person: the person's height (including stool) is not less than the height of the person immediately in front? That would be a different condition: B_i >= B_{i-1}. That also yields the same solution (since B_i >= B_{i-1} and B_{i-1} already >= all before, so it's equivalent). So yes.
Thus we can answer.
But before finalizing, I should consider any potential pitfalls: integer overflow: N up to 2e5, A_i up to 1e9, the maximum total stools could be if A = [1e9, 1, 1, ..., 1]? Actually first person is 1e9, all others are 1. Then each other needs stool = 1e9-1 ~ 1e9, times (N-1) up to 2e5, so sum up to 2e14, which fits in 64-bit (max ~9e18). So long long is safe.
So final answer.
But maybe there is an alternative interpretation: "Nobody in front of the person is taller than the person." Could also mean that for each person, all persons in front must be not taller (i.e., they are shorter or equal). But that is exactly the same as above.
Thus solution.
Now I'll write the final answer with explanation and code.
Since the user asked to "print the minimum total height of the stools", I'll provide the solution in Python as well? Usually they might expect code. The problem seems from AtCoder ABC (maybe "Stools" or something). But we can provide solution in C++ or Python. The instructions: "We need to produce the final answer." Typically we output code.
Given it's a standard problem, I'll produce a C++ solution.
But the system may want a textual explanation. The user likely wants a solution explanation. The prompt: "We need to understand the problem..." Actually the user is ChatGPT, but the final answer should include explanation and code? Usually for these problems, the response includes the reasoning and then the code.
I'll produce a concise explanation and then the code.
Thus final answer. We need to assign non‑negative stool heights to each person so that the total heights (original + stool) form a non‑decreasing sequence from front to back.
For each person we must have: total_i ≥ max{ total_j | j < i }.
The minimal way is to process from front to back, keeping the current maximum total height.
For the first person we use no stool. For each later person we raise its total to max(original_i, current_max).
The needed stool is max(0, current_max - original_i), and we update current_max accordingly.
Summing these differences gives the minimum total stool height.
cpp
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
vector<ll> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
ll cur = A[0]; // current maximum total height
ll ans = 0;
for (int i = 1; i < N; ++i) {
if (A[i] < cur) ans += cur - A[i];
else cur = A[i];
}
cout << ans << endl;
return 0;
}