fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #define INF 1000000000
  4.  
  5. using namespace std;
  6.  
  7. // you may add more methods here
  8.  
  9.  
  10. int solve(int n,int tree[511],int i,int& ans) {
  11. if(2*i > n && 2*i+1 > n){
  12. ans = max(ans,tree[i]);
  13. return tree[i];
  14. }
  15.  
  16. int left = -INF,right = -INF;
  17. if(2*i <= n)
  18. left = solve(n,tree,2*i,ans);
  19. if(2*i+1 <= n)
  20. right = solve(n,tree,2*i+1,ans);
  21. ans = max(ans,left+tree[i]+right);
  22. return tree[i] + max(left,right);
  23. }
  24.  
  25. int maxDiameterSum(int nodes, int tree[511]) {
  26. //add code to this function to solve the problem
  27. //don't write code in this editor
  28. //your code will not be saved across submissions
  29. int ans = -1000000000;
  30. return solve(nodes,tree,1,&ans);
  31. return 0;
  32. }
  33.  
  34. int main() {
  35. int T, N, A[511];
  36. int i;
  37. scanf("%d",&T);
  38. while(T--) {
  39. ans = -INF;
  40. scanf("%d",&N);
  41. for(i=0;i<N;i++) {
  42. scanf("%d",&A[i]);
  43. }
  44. printf("%d\n",maxDiameterSum(N,A));
  45. }
  46. return 0;
  47. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int solve(int, int*, int, int&)’:
prog.cpp:12:26: error: ‘max’ was not declared in this scope
     ans = max(ans,tree[i]);
                          ^
prog.cpp:21:36: error: ‘max’ was not declared in this scope
    ans = max(ans,left+tree[i]+right);
                                    ^
prog.cpp: In function ‘int maxDiameterSum(int, int*)’:
prog.cpp:30:31: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
     return solve(nodes,tree,1,&ans);
                               ^~~~
prog.cpp:10:5: note:   initializing argument 4 of ‘int solve(int, int*, int, int&)’
 int solve(int n,int tree[511],int i,int& ans) {
     ^~~~~
prog.cpp:30:31: error: cannot bind rvalue ‘(int)(& ans)’ to ‘int&’
     return solve(nodes,tree,1,&ans);
                               ^~~~
prog.cpp: In function ‘int main()’:
prog.cpp:39:6: error: ‘ans’ was not declared in this scope
      ans = -INF;
      ^~~
stdout
Standard output is empty