 # Getting WA in MNMX

I am getting WA for the problem MNMX. Could any one please tell me where I am going wrong ?Here is my code:

``````int main() {

int t;
cin>>t;

while ( t-- ) {
int n;
cin>>n;
vector<int> a,b;
int sum = 0;

for ( int i = 0 ; i < n; i++ ) {
int tmp;
cin>>tmp;
a.push_back(tmp);
}
while ( true ) {
int len = a.size();
b.clear();
if ( len <= 1 ) {
break;
}
for ( int i = 0; i < len-1 ; i+=2 ) {
if ( a[i] <= a[i+1]) {
sum += a[i];
b.push_back(a[i]);
}
else {
sum += a[i+1];
b.push_back(a[i+1]);
}
}
if ( len % 2 != 0 ) {
b.push_back(a[len-1]);
}
a.clear();
a = b;
}
cout<<sum<<endl;

}

return 0;
}``````

The bug in the code is that you are wrongly assuming that the min cost will be found by summing up the minimum of every 2 adjacent elements when the array is traversed from left to right.

Instead, the correct logic is to notice that the minimum will inadvertently be compared with the n-1 other elements in the process of deciding it is the minimum and since the minimum is the least value in the array, it will contribute the least cost. Hence, you must modify your program to find the min element in the array and multiply it with (n-1) to obtain the min cost.

//