Advertisement
artemgf

Maximum Sum O(n^2*log^2n)

Nov 29th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <cmath>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stack>
  13. #include <climits>
  14. #include <deque>
  15. #include <ctime>
  16. #include <iterator>
  17.  
  18. using namespace std;
  19. const int INF = (int)(2e9);
  20.  
  21. typedef long long ll;
  22. typedef unsigned long long ull;
  23. typedef unsigned int ui;
  24.  
  25. #define mh() make_heap()
  26. #define poph() pop_heap()
  27. #define pushh() push_heap()
  28. #define sor(n) n.begin(), n.end()
  29. #define mp make_pair
  30.  
  31.  
  32. #define files freopen("secretroom.in", "rt", stdin); freopen("secretroom.out", "wt", stdout)
  33. int main()
  34. {
  35.     ll n;
  36.     cin >> n;
  37.     int arr[101][101];
  38.     int min = -1e9;
  39.     for (int i = 1; i <= n; i++)
  40.     {
  41.         arr[i][0] = 0;
  42.         for (int j = 1; j <= n; j++)
  43.         {
  44.             int prom;
  45.             cin >> prom;
  46.             if (prom < 0)
  47.                 if (prom > min)
  48.                     min = prom;
  49.             arr[i][j] = arr[i][j - 1] + prom;
  50.         }
  51.     }
  52.  
  53.     int Max = -1e8;
  54.     for(int i=1;i<=n;i++)
  55.         for (int j = i; j <= n; j++)
  56.         {
  57.             ll maxx = 0;
  58.             for (int o = 1; o <= n; o++)
  59.             {
  60.                 maxx = 0;
  61.                 for (int u = o; u <= n; u++)
  62.                 {
  63.                     maxx += arr[u][j] - arr[u][i - 1];
  64.                     if (maxx > Max)
  65.                         Max = maxx;
  66.                 }
  67.                
  68.             }
  69.         }
  70.  
  71.     cout << max(Max,min);
  72.     system("pause");
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement