Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Solution {
- private final int MIN_VALUE = Integer.MIN_VALUE / 2;
- private void solution() throws IOException {
- int n = in.nextInt();
- int m = in.nextInt();
- int[][] p = new int[m][n];
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- p[j][i] = in.nextInt();
- }
- }
- int[][] dp = new int[m][n];
- for (int[] a : dp) {
- Arrays.fill(a, MIN_VALUE);
- }
- solve(m - 1, n - 1, p, dp);
- out.println(dp[m - 1][n - 1]);
- List<Integer> ans = new ArrayList<Integer>();
- int sum = dp[m - 1][n - 1];
- int x = m - 1;
- for (int y = n - 1; y >= 0 && x >= 0; --y) {
- while ((x - 1) >= 0 && solve(x - 1, y, p, dp) == sum) {
- --x;
- }
- ans.add(x);
- sum -= p[x][y];
- --x;
- }
- Collections.reverse(ans);
- for (int i = 0; i < ans.size(); ++i) {
- if (i != 0) {
- out.print(' ');
- }
- out.print(ans.get(i) + 1);
- }
- out.println();
- out.flush();
- }
- private int solve(int pos, int last, int[][] p, int[][] dp) {
- if (last < 0) {
- return 0;
- }
- if (pos < 0 && last >= 0) {
- return MIN_VALUE;
- }
- if (dp[pos][last] != MIN_VALUE) {
- return dp[pos][last];
- }
- int res = MIN_VALUE;
- for (int i = pos; i >= 0; --i) {
- int cur = solve(i - 1, last - 1, p, dp) + p[i][last];
- if (cur > res) {
- res = cur;
- }
- }
- return dp[pos][last] = res;
- }
- private class Scanner {
- BufferedReader reader;
- StringTokenizer tokenizer;
- public Scanner(Reader reader) {
- this.reader = new BufferedReader(reader);
- this.tokenizer = new StringTokenizer("");
- }
- public boolean hasNext() throws IOException {
- while (!tokenizer.hasMoreTokens()) {
- String next = reader.readLine();
- if (next == null) {
- return false;
- }
- tokenizer = new StringTokenizer(next);
- }
- return true;
- }
- public String next() throws IOException {
- hasNext();
- return tokenizer.nextToken();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(next());
- }
- public String nextLine() throws IOException {
- tokenizer = new StringTokenizer("");
- return reader.readLine();
- }
- }
- public static void main(String[] args) throws IOException {
- (new Solution()).solution();
- }
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- Scanner in = new Scanner(new InputStreamReader(System.in));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement