a_pramanik

max sum

Mar 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. ///:-)
  2. #include <bits/stdc++.h>
  3. #define pb push_back
  4. #define ll long long int
  5. #define inf 2000000000
  6. #define infLL 9000000000000000000
  7. #define infULL 18446744073709551615
  8. #define Aktaruzzaman using
  9. #define pi (2.0*acos(0.0))
  10. #define Pramanik namespace std;
  11. #define vsort(v)   sort(v.begin(),v.end())
  12. #define ull unsigned long long int
  13. #define mem(a, b) memset(a, b, sizeof a)
  14. #define cf 100009
  15. #define MOD 1000000007
  16. #define pii pair<int, int>
  17.  
  18. //int dx[]={0,1,0,-1};
  19. //int dy[]={1,0,-1,0};
  20. //int dx[]={1,1,0,-1,-1,-1,0,1};
  21. //int dy[]={0,1,1,1,0,-1,-1,-1};
  22.  
  23. template <class T> inline T gcd(T a,T b)
  24. {
  25.     if(b==0)return a;
  26.     return gcd(b,a%b);
  27. }
  28. template< class T > inline T lcm(T a,T b)
  29. {
  30.     a=abs(a);
  31.     b=abs(b);
  32.     return (a/gcd(a,b))*b;
  33. }
  34. template <class T> inline T is_prime(T a)
  35. {
  36.     if(a<2|a==4)return false;
  37.     if(a==2||a==3||a==5)return true;
  38.     T g=(T)sqrt(a)+1;
  39.     for(T i=2; i<=g; i++)
  40.     {
  41.         if(a%i==0)return false;
  42.     }
  43.     return false;
  44. }
  45. template <class T> inline T bigmod(T n,T p,T m)
  46. {
  47.     if(p==0)return 1;
  48.     if(p%2)return ((n%m)*bigmod(n,p-1,m))%m;
  49.     else
  50.     {
  51.         T c=bigmod(n,p/2,m);
  52.         return ((c%m)*(c%m))%m;
  53.     }
  54. }
  55.  
  56. Aktaruzzaman Pramanik
  57.  
  58. ll a[102][102], sum[102];
  59.  
  60. int main()
  61.  
  62. {
  63. //    ios_base:: sync_with_stdio(0);
  64. //    cin.tie(NULL);
  65. //    cout.tie(NULL);
  66.  
  67.     ll n, i, j, k, x, y, mx;
  68.  
  69.     while(scanf("%lld", &n)==1){
  70.     mx = (-1)*infLL;
  71.     for(i=1; i<=n; i++)
  72.     {
  73.         for(j=1; j<=n; j++)
  74.         {
  75.             cin>>a[i][j];
  76.             mx = max(mx, a[i][j]);
  77.         }
  78.     }
  79.  
  80.     if(mx<0)
  81.     {
  82.         printf("%lld\n", mx);
  83.         return 0;
  84.     }
  85.     ll ans = (-1)*infLL;
  86.     for(i=1; i<=n; i++)
  87.     {
  88.  
  89.         mem(sum, 0);
  90.  
  91.         for(j=i; j<=n; j++)
  92.         {
  93.  
  94.             x = 0;
  95.  
  96.             for(k=1; k<=n; k++)
  97.             {
  98.                 sum[k]+=a[k][j];
  99.                 x+=sum[k];
  100.                 if(x<0)x=0;
  101.                 ans=max(x, ans);
  102.  
  103.             }
  104.  
  105.         }
  106.  
  107.     }
  108.     if(ans==0)ans = mx;
  109.     printf("%lld\n", ans);
  110. }
  111.     return 0;
  112. }
Add Comment
Please, Sign In to add comment