Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Solution {
  5.  
  6.     private final int MIN_VALUE = Integer.MIN_VALUE / 2;
  7.     private void solution() throws IOException {
  8.         int n = in.nextInt();
  9.         int m = in.nextInt();
  10.         int[][] p = new int[m][n];
  11.         for (int i = 0; i < n; ++i) {
  12.             for (int j = 0; j < m; ++j) {
  13.                 p[j][i] = in.nextInt();
  14.             }
  15.         }
  16.         int[][] dp = new int[m][n];
  17.         for (int[] a : dp) {
  18.             Arrays.fill(a, MIN_VALUE);
  19.         }
  20.        
  21.         solve(m - 1, n - 1, p, dp);
  22.         out.println(dp[m - 1][n - 1]);
  23.         List<Integer> ans = new ArrayList<Integer>();
  24.         int sum = dp[m - 1][n - 1];
  25.         int x = m - 1;
  26.         for (int y = n - 1; y >= 0 && x >= 0; --y) {
  27.             while ((x - 1) >= 0 && solve(x - 1, y, p, dp) == sum) {
  28.                 --x;
  29.             }
  30.             ans.add(x);
  31.             sum -= p[x][y];
  32.             --x;
  33.         }
  34.         Collections.reverse(ans);
  35.         for (int i = 0; i < ans.size(); ++i) {
  36.             if (i != 0) {
  37.                 out.print(' ');
  38.             }
  39.             out.print(ans.get(i) + 1);
  40.         }
  41.         out.println();
  42.         out.flush();
  43.     }
  44.  
  45.     private int solve(int pos, int last, int[][] p, int[][] dp) {
  46.         if (last < 0) {
  47.             return 0;
  48.         }
  49.         if (pos < 0 && last >= 0) {
  50.             return MIN_VALUE;
  51.         }
  52.         if (dp[pos][last] != MIN_VALUE) {
  53.             return dp[pos][last];
  54.         }
  55.         int res = MIN_VALUE;
  56.         for (int i = pos; i >= 0; --i) {
  57.             int cur = solve(i - 1, last - 1, p, dp) + p[i][last];
  58.             if (cur > res) {
  59.                 res = cur;
  60.             }
  61.         }
  62.         return dp[pos][last] = res;
  63.     }
  64.  
  65.     private class Scanner {
  66.         BufferedReader reader;
  67.         StringTokenizer tokenizer;
  68.  
  69.         public Scanner(Reader reader) {
  70.             this.reader = new BufferedReader(reader);
  71.             this.tokenizer = new StringTokenizer("");
  72.         }
  73.  
  74.         public boolean hasNext() throws IOException {
  75.             while (!tokenizer.hasMoreTokens()) {
  76.                 String next = reader.readLine();
  77.                 if (next == null) {
  78.                     return false;
  79.                 }
  80.                 tokenizer = new StringTokenizer(next);
  81.             }
  82.             return true;
  83.         }
  84.  
  85.         public String next() throws IOException {
  86.             hasNext();
  87.             return tokenizer.nextToken();
  88.         }
  89.  
  90.         public int nextInt() throws IOException {
  91.             return Integer.parseInt(next());
  92.         }
  93.  
  94.         public String nextLine() throws IOException {
  95.             tokenizer = new StringTokenizer("");
  96.             return reader.readLine();
  97.         }
  98.     }
  99.  
  100.     public static void main(String[] args) throws IOException {
  101.         (new Solution()).solution();
  102.     }
  103.    
  104.     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  105.     Scanner in = new Scanner(new InputStreamReader(System.in));
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement