fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int A[][] = new int[105][105];
  11. static int Z[][] = new int[105][4];
  12. public static int min(int a,int b) {
  13. if(a<b) return a;
  14. return b;
  15. }
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18. String tt = in.readLine();
  19. int t = Integer.parseInt(tt);
  20. String ss = in.readLine();
  21. int as = Integer.parseInt(ss);
  22. String nn[];
  23. for(int z=1;z<=t;++z) {
  24. nn = in.readLine().split(" ");
  25. for(int i=0;i<as;++i)
  26. A[z-1][i]=Integer.parseInt(nn[i]);
  27. }
  28.  
  29. Z[0][0]=A[0][0];
  30. Z[0][1]=A[0][1];
  31. Z[0][2]=A[0][2];
  32. for(int i=1;i<t;++i) {
  33. Z[i][0]=min(Z[i-1][1],Z[i-1][2])+A[i][0];
  34. Z[i][1]=min(Z[i-1][0],Z[i-1][2])+A[i][1];
  35. Z[i][2]=min(Z[i-1][0],Z[i-1][1])+A[i][2];
  36. }
  37. System.out.println(min(min(Z[t-1][0],Z[t-1][1]),Z[t-1][2]));
  38. }
  39. }
Success #stdin #stdout 0.06s 32928KB
stdin
3
3
1 2 2
2 1 2
2 2 1
stdout
3