|
| 1 | +package contest; |
| 2 | + |
| 3 | +import Utils.BladeReader; |
| 4 | + |
| 5 | +import java.io.PrintWriter; |
| 6 | + |
| 7 | +public class TaskTorus { |
| 8 | + public void solve(int testNumber, BladeReader in, PrintWriter out) { |
| 9 | + int size = in.nextInt(); |
| 10 | + int[][] mat = in.readIntMatrix(size, size); |
| 11 | + int[][] ext = new int[(size << 1) - 1][(size << 1) - 1]; |
| 12 | + for (int i = 0; i < (size << 1) - 1; i++) { |
| 13 | + for (int j = 0; j < (size << 1) - 1; j++) { |
| 14 | + ext[i][j] = mat[i >= size ? (i - size) : i][j >= size ? (j - size) : j]; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | +// System.out.println("Extended Matrix"); |
| 19 | +// new PrintMat(ext); |
| 20 | + solve(ext, out, size); |
| 21 | + } |
| 22 | + |
| 23 | + private void solve(int[][] ext, PrintWriter out, int N) { |
| 24 | + long maxSum = Long.MIN_VALUE; |
| 25 | + int[][] costMatrix = getCostMatrix(ext); |
| 26 | + for (int colStart = 0; colStart < N; colStart++) { |
| 27 | + for (int colEnd = colStart; colEnd < colStart + N; colEnd++) { |
| 28 | + int[] currentVector = getCurrentVector(ext, colStart, colEnd, costMatrix); |
| 29 | + maxSum = Math.max(maxSum, kadane(currentVector, N)); |
| 30 | + } |
| 31 | + } |
| 32 | + out.println(maxSum); |
| 33 | + } |
| 34 | + |
| 35 | + private long kadane(int[] currentVector, int N) { |
| 36 | + // Limit the rows |
| 37 | + long max = Long.MIN_VALUE; |
| 38 | + long sum = 0; |
| 39 | + boolean vis[] = new boolean[currentVector.length]; |
| 40 | + for (int i = 0; i < currentVector.length; i++) { |
| 41 | + if (i >= N && vis[i - N]) { |
| 42 | + sum -= currentVector[i - N]; |
| 43 | + vis[i - N] = false; |
| 44 | + for (int x = i - N + 1; x < i && !vis[x - 1]; x++) |
| 45 | + if (currentVector[x] < 0) { |
| 46 | + sum -= currentVector[x]; |
| 47 | + vis[x] = false; |
| 48 | + } |
| 49 | + max = Math.max(max, sum); |
| 50 | + } |
| 51 | + sum += currentVector[i]; |
| 52 | + vis[i] = true; |
| 53 | + max = Math.max(max, sum); |
| 54 | + if (sum < 0) { |
| 55 | + sum = 0; |
| 56 | + vis[i] = false; |
| 57 | + } |
| 58 | + } |
| 59 | + return max; |
| 60 | + } |
| 61 | + |
| 62 | + private int[] getCurrentVector(int[][] ext, int colStart, int colEnd, int[][] costMatrix) { |
| 63 | + int[] currentVector = new int[ext.length]; |
| 64 | + for (int i = 0; i < ext.length; i++) { |
| 65 | + currentVector[i] = costMatrix[i][colEnd] - (colStart > 0 ? costMatrix[i][colStart - 1] : 0); |
| 66 | + } |
| 67 | +// System.out.println("Current Vector"); |
| 68 | +// System.out.println(Arrays.toString(currentVector)); |
| 69 | + return currentVector; |
| 70 | + } |
| 71 | + |
| 72 | + private int[][] getCostMatrix(int[][] ext) { |
| 73 | + int[][] costMatrix = new int[ext.length][ext[0].length]; |
| 74 | + for (int i = 0; i < ext.length; i++) { |
| 75 | + for (int j = 0; j < ext[0].length; j++) { |
| 76 | + costMatrix[i][j] = (j > 0 ? costMatrix[i][j - 1] : 0) + ext[i][j]; |
| 77 | + } |
| 78 | + } |
| 79 | +// System.out.println("Cost Matrix"); |
| 80 | +// new PrintMat(costMatrix); |
| 81 | + return costMatrix; |
| 82 | + } |
| 83 | +} |
| 84 | + |
0 commit comments