fork(2) download
  1.  
  2. //import static java.lang.Integer.parseInt;
  3. import java.util.*;
  4. import java.io.*;
  5. class BYTESM2
  6. {
  7. public static void main(String args[])
  8. throws IOException
  9. {
  10. int arr[][]=new int[111][111];
  11. //PrintWriter pw=new PrintWriter(System.out);
  12. int t=Integer.parseInt(br.readLine().trim());
  13. int r,c;
  14. while(t--!=0)
  15. {
  16. //refresing the array
  17. for(int[] row: arr)
  18. Arrays.fill(row,0);
  19. //
  20.  
  21. String in[]=br.readLine().trim().split(" ");
  22. r=Integer.parseInt(in[0]);
  23. c=Integer.parseInt(in[1]);
  24. //arr=new int[r][c+2];
  25.  
  26. for(int i=0;i<r;i++)
  27. {
  28. String str=br.readLine().trim();
  29. int pointer=1;
  30. for(String s: str.split(" "))
  31. {
  32. //System.out.println("hamba "+s);
  33. arr[i][pointer++]=Integer.parseInt(s);
  34. }
  35. //System.out.println("pointer "+pointer);
  36. arr[i][0] = arr[i][pointer] = -1;
  37. }
  38. //calculation part
  39. int big=0;
  40. // if just one row is present
  41. /*if(r==1)
  42. {
  43. for(int j=0;j<c;j++)
  44. {
  45. if(arr[0][j]>big)
  46. big=arr[0][j];
  47. }
  48. System.out.println(big);
  49. continue;
  50. }
  51. else if(c==1)// if just one column is present
  52. {
  53. for(int i=0;i<r;i++)
  54. big+=arr[i][0];
  55. System.out.println(big);
  56. continue;
  57. }
  58. else if(r==1 && c==1)
  59. {
  60. System.out.println(arr[0][0]);
  61. continue;
  62.  
  63. }*/
  64.  
  65.  
  66.  
  67. for(int i=1;i<r;i++)
  68. {
  69. for(int j=1;j<=c;j++)
  70. {
  71. /*if(j==0)
  72. arr[i][j]+=Math.max(arr[i+1][j],arr[i+1][j+1]);
  73. else if(j==c-1)
  74. arr[i][j]+=Math.max(arr[i+1][j],arr[i+1][j-1]);*/
  75. //else
  76. //{
  77. int max1=Math.max(arr[i-1][j-1], arr[i-1][j]);
  78. int max=Math.max(max1,arr[i-1][j+1]);
  79. arr[i][j]+=max;
  80. //}
  81.  
  82. //if(arr[i][j]>big)
  83. //big=arr[i][j];
  84. }
  85. }
  86. for(int j = 1; j <= c; j++)
  87. big = Math.max(big, arr[r-1][j]);
  88. // print big
  89. System.out.println(big);
  90.  
  91.  
  92. }
  93.  
  94.  
  95. }
  96. }
Success #stdin #stdout 0.06s 380224KB
stdin
3
6 5
3 1 7 4 2
2 1 3 1 1
1 2 2 1 8
2 2 1 5 3
2 1 4 4 4
5 2 7 5 1
1 5
2 5 3 6 9
5 1
2
3
4
5
6
stdout
32
9
20