108


SUBMITTED BY: Guest

DATE: Dec. 25, 2013, 3:01 p.m.

FORMAT: Text only

SIZE: 2.5 kB

HITS: 1884

  1. #include <algorithm>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <limits.h>
  8. #include <map>
  9. #include <queue>
  10. #include <set>
  11. #include <stack>
  12. #include <string>
  13. #include <vector>
  14. using namespace std;
  15. typedef pair<int,int> ii;
  16. typedef vector<int> vi;
  17. typedef vector<ii> vii;
  18. typedef set<int> si;
  19. typedef set<ii> sii;
  20. #define MP make_pair
  21. #define PB push_back
  22. #define REP(i,a) for (int i = 0; i < int(a); i++)
  23. #define FOR(i,a,b) for ( int i = int(a); i <= int(b); ++i )
  24. #define FORD(i,a,b) for ( int i = int(a); i >= int(b); --i )
  25. #define TR(c,it) for( typeof(c.begin()) it = c.begin(); it != c.end(); ++it )
  26. #define TRR(c,it) for( typeof(c.rbegin()) it = c.rbegin(); it != c.rend(); ++it )
  27. const int INF = 1<<29;
  28. typedef long long int lli;
  29. const int MAXROW = 100;
  30. const int MAXCOL = 100;
  31. int matrix[MAXROW][MAXCOL];
  32. int kadane( int * arr, int & start, int & finish, int n )
  33. {
  34. int sum = 0, maxSum = INT_MIN;
  35. finish = -1;
  36. int local_start = 0;
  37. FOR( i, 0, n-1 )
  38. {
  39. sum += arr[i];
  40. if ( sum < 0 )
  41. {
  42. sum = 0;
  43. local_start = i + 1;
  44. }
  45. else if ( sum > maxSum )
  46. {
  47. maxSum = sum;
  48. start = local_start;
  49. finish = i;
  50. }
  51. }
  52. if ( finish != -1 )
  53. return maxSum;
  54. maxSum = arr[0];
  55. start = finish = 0;
  56. FOR( i, 1, n-1 )
  57. {
  58. if ( sum > maxSum )
  59. {
  60. maxSum = sum;
  61. start = finish = i;
  62. }
  63. }
  64. return maxSum;
  65. }
  66. int findMaxSum( int row, int col )
  67. {
  68. int maxSum = INT_MIN, finalLeft, finalRight, finalTop, finalBottom;
  69. int left, right;
  70. int temp[row], sum, start, finish;
  71. FOR( left, 0, col - 1 )
  72. {
  73. memset( temp, 0, sizeof(temp) );
  74. FOR( right, left, col-1 )
  75. {
  76. FOR( i, 0, row-1 )
  77. temp[i] += matrix[i][right];
  78. sum = kadane( temp, start, finish, row );
  79. if ( sum > maxSum )
  80. {
  81. maxSum = sum;
  82. finalLeft = left;
  83. finalRight = right;
  84. finalTop = start;
  85. finalBottom = finish;
  86. }
  87. maxSum = max( sum, maxSum );
  88. }
  89. }
  90. return maxSum;
  91. }
  92. int main( )
  93. {
  94. int n;
  95. while ( scanf("%d", &n) == 1 )
  96. {
  97. FOR( i, 0, n-1 )
  98. FOR( j, 0, n-1 )
  99. scanf("%d", &matrix[i][j]);
  100. printf("%d\n", findMaxSum( n, n ));
  101. }
  102. return 0;
  103. }

comments powered by Disqus