codeforces.com/contest/426/problem/D
const int MAXN = 110;int ipt[MAXN][MAXN];int main(){// freopen("in.txt", "r", stdin); int n, m, k; while (~RIII(n, m, k)) { REP(i, n) REP(j, m) RI(ipt[i][j]); if (n < m) { REP(i, n) FF(j, i + 1, m) swap(ipt[i][j], ipt[j][i]); swap(n, m); } if (n > k) { int ans = INF; REP(i, n) { int tans = 0; REP(j, n) { int cnt = 0; if (i == j) continue; REP(k, m) { if (ipt[i][k] != ipt[j][k]) cnt++; } tans += min(cnt, m - cnt); } ans = min(ans, tans); } printf("%d\n", ans <= k ? ans: -1); } else { int ans = INF; REP(i, n) { int all = 1 << m; for (int q = 0; q < all; q++) { int diff = 0; for (int t = 0, l = 1; t < m; l <<= 1, t++) if (((q & l) != 0) != ipt[i][t]) diff++; if (diff > k) continue; int tans = 0; REP(j, n) { if (i == j) continue; int cnt = 0; for (int t = 0, l = 1; t < m; t++, l <<= 1) if (((q & l) != 0) != ipt[j][t]) cnt++; tans += min(cnt, m - cnt); } ans = min(ans, diff + tans); } } printf("%d\n", ans <= k ? ans: -1); } } return 0;}