Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. long long FindMax(int size, long long **A)
  4. {
  5.     long long max = 0;
  6.  
  7.     for (int j = 0; j < size; ++j)
  8.     {
  9.         if (A[0][size - 1] + A[1][j] > max)
  10.         {
  11.             max = A[0][size - 1] + A[1][j];
  12.         }
  13.         else
  14.             if (A[0][size - 1] - A[2][j] > max)
  15.             {
  16.                 max = A[0][size - 1] - A[2][j];
  17.             }
  18.     }
  19.  
  20.     return max;
  21. }
  22.  
  23. void SetTables(int j, long long **A)
  24. {
  25.     if (j != 0)
  26.     {
  27.         if (A[1][j - 1] > 0)
  28.         {
  29.             A[1][j] = A[1][j - 1] + A[0][j];
  30.         }
  31.         else
  32.         {
  33.             A[1][j] = A[0][j];
  34.         }
  35.  
  36.         if (A[2][j - 1] < 0)
  37.         {
  38.             A[2][j] = A[2][j - 1] + A[0][j];
  39.         }
  40.         else
  41.         {
  42.             A[2][j] = A[0][j];
  43.         }
  44.  
  45.         A[0][j] += A[0][j - 1];
  46.     }
  47.     else
  48.         if (j == 0)
  49.         {
  50.             A[1][j] = A[0][j];
  51.             A[2][j] = A[0][j];
  52.         }
  53. }
  54.  
  55. int main()
  56. {
  57.     //std::ios_base::sync_with_stdio(0);
  58.     //std::cin.tie(nullptr);
  59.     //std::cout.tie(nullptr);
  60.  
  61.     long long n, m, max = 0;
  62.     std::cin >> n;
  63.     for (int i = 0; i < n; ++i)
  64.     {
  65.         std::cin >> m;
  66.         long long ** T = new long long * [3];
  67.         T[0] = new long long[m];
  68.         T[1] = new long long[m];
  69.         T[2] = new long long[m];
  70.         for (int j = 0; j < m - 1; ++j)
  71.         {
  72.             std::cin >> T[0][j];
  73.             SetTables(j, T);
  74.         }
  75.         std::cout << FindMax(m - 1, T) << "\n";
  76.         delete [] T[0], T[1], T[2], T;
  77.     }
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement