fork download
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. long long int solve(int op[],int n)
  8. {
  9. int i,j,arr[n];
  10. long long int ans1=0,ans2=0,sum[n];
  11. ans1=0;//waste var in this approach- was used in other approach.
  12. sum[0]=0;
  13. arr[0]=abs(op[0]-op[1])>op[1]-1?op[0]:1;/*Initialising arr[0] as op[0] if it maximises the difference b/w op[1] & op[0]. Else its given 1 by default. */
  14. //cout<<arr[0]<<" "; - Used while debugging. Ignore.
  15. for(i=1;i<n;i++)
  16. {
  17. arr[i]= abs(op[i]-arr[i-1])>abs(arr[i-1]-1)?op[i]:1; /*An attempt to construct ideal array by giving arr[i] the value of op[i] if the diff b/w previous element and op[i] is more than what it would be if arr[i] was assigned 1. */
  18. sum[i]= sum[i-1] + abs(arr[i]-arr[i-1]); /*Sum array WAS used to directly find max sum in previos approach (which miserably failed). Lol. Retained array in case an idea strikes me in future.*/
  19. //cout<<arr[i]<<" ";-used for debugging
  20. }
  21. //cout<<endl;-used for debugging.
  22. return sum[n-1];
  23. }
  24.  
  25. int main() {
  26. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  27. int t;
  28. cin>>t;//test cases
  29. while(t--)
  30. {
  31. int n;//size of array
  32. cin>>n;
  33. int arr[n];//Baow-Chiki-Baow-Baow.
  34. int i,j,k;
  35. long long int sum=0;//I think i used it to store final ans of sum. I guess so... :p
  36. for(i=0;i<n;i++)
  37. cin>>arr[i];
  38. sum = solve(arr,n);//Function call if i am not wrong
  39. cout<<sum<<endl;//I am definetely sure i used it to print output. :p XD
  40. }
  41. return 0; //Typical return 0;
  42. }
Success #stdin #stdout 0s 15240KB
stdin
1
5
10 1 10 1 10
stdout
36