赤緑青のボールを入れる箱をそれぞれ少なくとも1つは選ばなくてはいけない。
その1つを選び、残りは最も手数の少なくて済む色にしてしまう。
class BallsSeparating { public: int minOperations(vector <int> R, vector <int> G, vector <int> B) { const int N = R.size(); const int inf = 1 << 29; int ret = inf; int costR[N]; int costG[N]; int costB[N]; for (int i = 0; i < N; ++i) { costR[i] = G[i] + B[i]; costG[i] = R[i] + B[i]; costB[i] = G[i] + R[i]; } for (int r = 0; r < N; ++r) { for (int g = 0; g < N; ++g) { for (int b = 0; b < N; ++b) { { set<int> cnt; cnt.insert(r); cnt.insert(g); cnt.insert(b); if (cnt.size() != 3) continue; } int cost = costR[r] + costG[g] + costB[b]; for (int i = 0; i < N; ++i) { if (i == r) continue; if (i == g) continue; if (i == b) continue; cost += min(costR[i], min(costG[i], costB[i])); } ret = min(ret, cost); } } } return ret == inf ? -1 : ret; } };