#include #include using namespace std; int tab [500]; int main(int argc, char *argv[]) { int cases; cin >> cases; for ( int caseNo = 0; caseNo < cases; caseNo ++) { int F; // the amount of fuel to safely land string path; cin >> F >> path; // tab [i] - minimal cost of getting from location i to the base tab [ path.size()-1] = 0; // you are already at the base for ( int i = path.size() - 2; i >= 0; i -- ) { int minCost = 500*500 + 101; for ( int j = i+1; j < path.size(); j ++) if ( path[j] != 'v' && (j-i) * (j-i) + F + tab[j] < minCost) minCost = (j-i) * (j-i) + F + tab[j]; tab[i] = minCost; } cout << tab[0] << endl; } return 0; }