fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef double ld;
  4. typedef long long int ll;
  5. #define MAX 1000005
  6. #define INF 1000000005
  7. #define mod 1000000007
  8. const double PI = 3.14159265;
  9. #define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL);
  10.  
  11. // normal 4 moves
  12. ll ndx[]={0,1,0,-1};
  13. ll ndy[]={1,0,-1,0};
  14. // all 8 moves
  15. ll qdx[]={-1,-1,-1,0,1,1,1,0};
  16. ll qdy[]={-1,0,1,1,1,0,-1,-1};
  17. // knight's moves
  18. ll kdx[]={-2,-2,2,2,-1,-1,1,1};
  19. ll kdy[]={-1,1,1,-1,-2,2,-2,2};
  20.  
  21. ll power(ll x, ll y, ll p)
  22. {
  23. ll res=1;
  24. x=x%p;
  25. while(y>0)
  26. {
  27. if(y&1)
  28. res=(res*x)%p;
  29. y=y>>1;
  30. x=(x*x)%p;
  31. }
  32. return res;
  33. }
  34.  
  35. ll n,a[MAX];
  36.  
  37. ll f(ll i,ll sum)
  38. {
  39. if(i==n-1) return sum;
  40. ll maxx=0;
  41. maxx=max(maxx,max(f(i+1,sum+abs(min(ll(1),a[i])-min(ll(1),a[i+1]))),f(i+1,sum+abs(min(ll(1),a[i])-max(ll(1),a[i+1])))));
  42. maxx=max(maxx,max(f(i+1,sum+abs(max(ll(1),a[i])-min(ll(1),a[i+1]))),f(i+1,sum+abs(max(ll(1),a[i])-max(ll(1),a[i+1])))));
  43. return maxx;
  44. }
  45.  
  46. int main()
  47. {
  48. ll t;
  49. cin>>t;
  50. while(t--)
  51. {
  52. cin>>n;
  53. for(ll i=0;i<n;i++) cin>>a[i];
  54. cout<<f(0,0)<<endl;
  55. }
  56. }
  57.  
Success #stdin #stdout 0s 4392KB
stdin
Standard input is empty
stdout
Standard output is empty