Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. void solve() throws IOException {
  2. int n = readInt();
  3. int size = n * (n + 1) / 2;
  4. this.graph = new List[size];
  5. for (int i = 0; i < size; i++) {
  6. graph[i] = new ArrayList<Integer>();
  7. }
  8.  
  9. for (int i = 0; i < n; i++) {
  10. for (int j = 0; j <= i; j++) {
  11. if (i * (i + 1) / 2 + j + i + 1 < size) {
  12. graph[i * (i + 1) / 2 + j].add(i * (i + 1) / 2 + j + i + 1);
  13. graph[i * (i + 1) / 2 + j].add(i * (i + 1) / 2 + j + i + 2);
  14. }
  15. }
  16.  
  17. }
  18. this.init = readIntArray(size);
  19. out.println(dfs(0));
  20. }
  21.  
  22. int[] init;
  23.  
  24. int dfs(int from) {
  25. int max = -100000;
  26. for (int i : graph[from]) {
  27. max = maxInt(dfs(i), max);
  28. }
  29. if (max == -100000) {
  30. max = 0;
  31. }
  32. return max + init[from];
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement