Just greedy. First think about the final balance, if you know the total weight, then the weight of each node is actually determined.
Each node in the left-to-right actually does not affect, and only the number of layers. Now in turn, if you do not modify a node, then you can calculate the total quality, the total quality of the occurrence of the highest number of times remain unchanged.
/**********************************************************--------------Tyrannosaurus---------* * Author Abyssalfish ***********************************************************/#include<bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intMaxd = -, LEN = (1<< the)* A+Ten;CharS[len];map<ll,int>CNT;//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif intT scanf"%d",&T); while(t--) {scanf ("%s", s); Char*p =s; intD =0; Cnt.clear (); while(*p) { if(IsDigit (*p)) {ll x= *p-'0'; while(IsDigit (* (++p))) x = x*Ten+*p-'0'; Cnt[x<<d]++; if(*p = =']') d--; if(!*p) Break; } Else if(*p = ='[') d++; P++; } intsum =0, M =0; for(auto pr:cnt) {sum+=Pr.second; M=Max (M,pr.second); } printf ("%d\n", sum-l); } return 0;}
UVA 12166 Equilibrium Mobile