Stone merger Ultimate Universal Edition
#include <bits / STDC ++ H.> the using namespace STD; int Stone [ 50010 ]; int n-, T, ANS; void Combine ( int K) { int TEM Stone = [K] + Stone [- K- . 1 ]; // the combined stack k and k-1 ANS = + TEM; for ( int I = k; I <T- . 1 ; I ++) Stone [I] = Stone [I + . 1 ]; // after k forward shift T-- ; int J; for (= - K-J . 1 ; J> 0 && Stone [J- . 1] <tem; J,) Stone [j] = Stone [J- . 1 ]; // K-back. 1 after the shift, to find a position greater than the tem Stone [j] = tem; // inserted at j TEM the while (J> = 2 && Stone [J- 2 ] <= Stone [J]) { // the new sequence obtained in recursive processing int D = TJ; // for backtracking. . . Combine (J- . 1 ); J = TD; // back } } int main() { while(~scanf("%d",&n)) { if(n==0) break; for(int i=0; i<n; i++) scanf("%d",&stone[i]); t=1; ans=0; for(int i=1; i<n; i++) { Stone [T ++] = Stone [I]; the while (T> = . 3 && Stone [T- . 3 ] <= Stone [T- 1 ]) Combine (T- 2 ); // from the beginning to the end of traverse } the while (T> . 1 ) Combine (T- . 1 ); // combined into a stack, if not after the first recombining the printf ( " % D \ n- " , ANS); } return 0; }