fork download
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws Exception
  7. {
  8. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  9. String[] params1 = bufferedReader.readLine().split(" ");
  10. int n = Integer.parseInt(params1[0]);
  11. int m = Integer.parseInt(params1[1]);
  12. int[][] A = new int[n][m];
  13. for (int i=0; i<n; i++) {
  14. String[] params2 = bufferedReader.readLine().split(" ");
  15. for (int j=0; j<m; j++) {
  16. A[i][j] = Integer.parseInt(params2[j]);
  17. }
  18. }
  19. for (int i=1; i<n; i++) {
  20. A[i][0]+=A[i-1][0];
  21. }
  22. for (int i=1; i<m; i++) {
  23. A[0][i]+=A[0][i-1];
  24. }
  25. for (int i=1; i<n; i++) {
  26. for (int j=1; j<m; j++) {
  27. A[i][j]+=Math.min(A[i-1][j],A[i][j-1]);
  28. }
  29. }
  30. System.out.println(A[n-1][m-1]);
  31. }
  32. }
Success #stdin #stdout 0.04s 2184192KB
stdin
1 5
1 1 2 2 1
stdout
7