#include using namespace std; const int MAXN = 1000; bool src[MAXN], dst[MAXN]; int costs[MAXN]; bool extra[MAXN]; // true if should make extra move on 1->1 node typedef long long LL; int main() { int ncase, n; LL s; // sum of bits going from 1 to 0 LL r; // sum of bits staying at 1; LL t; // sum of any src bit = 1; LL nchange; // number of bits where src != dst cin >> ncase; for(int icase=1; icase<=ncase; icase++) { cin >> n; for(int i=0; i> ch; src[i] = (ch == '1'); } for(int i=0; i> ch; dst[i] = (ch == '1'); } LL m=0; s = 0, r = 0, t = 0, nchange = 0; for(int i=0; i> val; if (src[i] || dst[i]) { if (src[i]) { t += val; if (!dst[i]) s += val; else r += val; } else if (dst[i]) s += val; if (src[i] != dst[i]) nchange++; LL j=m; LL savsrc = src[i]; LL savdst = dst[i]; while (j>0 && costs[j-1] < val) { costs[j] = costs[j-1]; src[j] = src[j-1]; dst[j] = dst[j-1]; j--; } costs[j] = val; src[j] = savsrc; dst[j] = savdst; m++; } extra[i] = false; } // process LL start = 0; LL change = 0; for(int i=0; i=0; i--) { if (!src[i] || extra[i]) { t += costs[i]; totcost += t; } } cout << "Case " << icase << ": " << totcost << endl; } }