#include #include #include #include #include #include #define Max(a, b) ((a) > (b) ? (a) : (b)) #define Min(a, b) ((a) > (b) ? (b) : (a)) #define abs(x) ((x) > 0 ? (x) : -(x)) #define MOD (ll)1000000007 //10^9 + 7 #define endl printf("\n") typedef long long ll; #define MAX_N (1 << 17) int d[5][5]; ll c[5][5]; bool cjudge(int x[5][5]) { int s = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { int z = 0, w = 0; for (int p = -1; p <= 1; p++) { for (int q = -1; q <= 1; q++) { if(p * q == 0 && 0 <= i + p && i + p < 5 && 0 <= j + q && j + q < 5 ) { z++; if (x[i][j] == 1 && x[i + p][j + q] == 0) w++; } } } if (z == w) return false; } } return true; } ll sum, ans = LLONG_MAX; void dfs(int i, ll r, bool sign) { if (i == 25 || r >= sum / 2) { ans = Min(ans, abs(sum - (ll)2 * r)); return; } int s = i / 5, t = i % 5; if (!sign) { d[s][t] = 1; dfs(i + 1, r + c[s][t], true); d[s][t] = 0; } else { for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x * y == 0 && 0 <= s + x && s + x < 5 && 0 <= t + y && t + y < 5 && d[s + x][t + y] == 1) { d[s][t] = 1; dfs(i + 1, r + c[s][t], true); d[s][t] = 0; break; } } } dfs(i + 1, r, true); } return; } int main(int argc, char *argv[]) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { scanf("%lld", &c[i][j]); sum += c[i][j]; } } dfs(0, 0, false); printf("%lld\n", ans); return 0; }