[Question]
  [
Define the "maximum sub-array" of a given array as "a (consecutive) sub-array that has the biggest sum". Note there's no "non-zero" requirement. Output that sum.
Give a description of your code if possible.
Sample input 1:
```
1 2 3 -4 -5 6 7 -8 9 10 -11 -12 -13 14
```
Sample output 1: `24`
Description 1:  
The biggest sum is yielded by cutting `6 7 -8 9 10` out and summing up.
Sample input 2: `-1 -2 -3`  
Sample output 2: `0`  
Description 2: It's simple :) An empty subarray is the "biggest".
Requirement:
* Don't read anything except stdin, and output should go to stdout.
* Standard loopholes **restrictions** apply.
Ranking: The shortest program wins this [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'").
  
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~6~~ 4 bytes
```
‚ñ≤·πÅ‚à´·π´
```
[Try it online!](https://tio.run/##yygtzv7//9G0TQ93Nj7qWP1w5@r///9HG@oY6Rjr6Jro6JrqmOmY6@ha6FjqGBro6BoaArEREBvrGJrEAgA "Husk ‚Äì Try It Online")
```
      -- implicit input (list) xs  - eg. [-1,2,3]
   ·π´  -- get all tails of xs       -     [[-1,2,3],[2,3],[3],[]]
 ·πÅ‚à´   -- map & concat cumsum       -     [0,-1,1,4,0,2,5,0,3,0]
‚ñ≤     -- get maximum               -     5
```
[Answer]
# [Python 3](https://docs.python.org/3/), 61 bytes
```
a=b=0
for x in eval(input()):a=max(x,a+x);b=max(a,b)
print(b)
```
[Try it online!](https://tio.run/##HcdNCoMwEAbQvafIMkO/gGPsnyUnkS4moDTQxiBpiadPxcVbvLTl1xJtreK8a5t5WVVRIarpJ28dYvpmTTSI@0jRBXIq9PBHBJ6atIaYtadaR0YHC9PDnHHBFeaGO7iFYd51Owvun38 "Python 3 ‚Äì Try It Online")
Algorithm stolen from [Wikipedia](https://en.wikipedia.org/wiki/Maximum_subarray_problem).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
·∫ÜS‚Ç¨;0·πÄ
```
[Try it online!](https://tio.run/##y0rNyan8///hrrbgR01rrA0e7mz4//9/tK6hjq6Rjq5xLAA "Jelly ‚Äì Try It Online")
[Answer]
# [Pyth](https://pyth.readthedocs.io), 8 bytes
```
eS+0sM.:
```
**[Try it online!](http://pyth.herokuapp.com/?code=eS%2B0sM.%3A&input=%5B-1%2C+-2%2C+-3%5D%0A%5B1%2C+2%2C+3%2C+-4%2C+-5%2C+6%2C+7%2C+-8%2C+9%2C+10%2C+-11%2C+-12%2C+-13%2C+14%5D&test_suite=1&test_suite_input=%5B-1%2C+-2%2C+-3%5D%0A%5B1%2C+2%2C+3%2C+-4%2C+-5%2C+6%2C+7%2C+-8%2C+9%2C+10%2C+-11%2C+-12%2C+-13%2C+14%5D&debug=0)**
---
# How?
```
eS+0sM.:Q - Q is implicit, meaning input. Let's say it's [-1, -2, -3].
      .:  - All contiguous non-empty sublists. We have [[-1], [-2], [-3], [-1, -2], [-2, -3], [-1, -2, -3]].
    sM    - Get the sum of each sublist. [-1, -2, -3, -3, -5, -6]
  +0      - Append a 0 to the sum list. [0, -1, -2, -3, -3, -5, -6]
eS        - Maximum element. S gives us [-6, -5, -3, -3, -2, -1, 0], while e returns 0, the last element.
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
```
√é≈í0M
```
[Try it online!](https://tio.run/##MzBNTDJM/f//cN/RSQa@//9H6xrrKOgaAbFhLAA "05AB1E ‚Äì Try It Online")
-1 thanks to [Adnan](https://codegolf.stackexchange.com/users/34388/adnan).
[Answer]
# [Haskell](https://www.haskell.org/), 28 bytes
```
maximum.scanl((max<*>).(+))0
```
[Try it online!](https://tio.run/##FYrLCsIwFER/ZRYuEr0tvbU@CtYfURchGA0mITQKfr3xdjGHM8w8TXndQ6huutZovj5@YlusSUEpqaf1Wbdqo3Uno0@YkGef3ljB4cKEnrAlNINkR9gTDmJHwkjgTpR5Qb9Afjzc6s@6YB6lNjbnPw "Haskell ‚Äì Try It Online")
[Answer]
## C++, ~~197~~ ~~195~~ 187 bytes
-10 bytes thanks to Zachar√Ω
```
#include<vector>
#include<numeric>
int f(std::vector<int>v){int i=0,j,t,r=0;for(;i<v.size();++i)for(j=i;j<v.size();++j){t=std::accumulate(v.begin()+i,v.begin()+j,0);if(t>r)r=t;}return r;}
```
[Answer]
# [R](https://www.r-project.org/), 54 bytes
```
a=b=0;for(x in scan()){a=max(x,a+x);b=max(a,b)};cat(b)
```
[Try it online!](https://tio.run/##HcdNCoAgEAbQ/XeKlg4lOPaPeJgxCFpkUC2E6OwmLd7inTmLD9649ThVqrZYXYtERfSI3yWp1EidyIU/0gR63SK3CpQZFi10B91jwAg9YQYbaObCFi24yx8 "R ‚Äì Try It Online")
Algorithm taken from: [Maximum subarray problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem)
# [R](https://www.r-project.org/), 65 bytes
```
y=seq(x<-scan());m=0;for(i in y)for(j in y)m=max(m,sum(x[i:j]));m
```
[Try it online!](https://tio.run/##K/r/v9K2OLVQo8JGtzg5MU9DU9M619bAOi2/SCNTITNPoVITxMyCMHNtcxMrNHJ1iktzNSqiM62yYkHK/xtyGXEZc@macOmacplxmXPpWnBZchkacOkaGgKxERAbcxma/AcA "R ‚Äì Try It Online")
* Read `x` from stdin.
* Set `y` as index of `x`.
* Iterate twice over all possible nonempty subsets.
* Compare sum of a subset with `m` (initially `m=0`).
* Store maximum value in `m`.
* Print value of `m`.
# [R](https://www.r-project.org/), 72 bytes
```
n=length(x<-scan());m=0;for(i in 1:n)for(j in i:n)m=max(m,sum(x[i:j]));m
```
[Try it online!](https://tio.run/##K/r/P882JzUvvSRDo8JGtzg5MU9DU9M619bAOi2/SCNTITNPwdAqTxPEyQJxMoGcXNvcxAqNXJ3i0lyNiuhMq6xYkJb/hlxGXMZcuiZcuqZcZlzmXLoWXJZchgZcuoaGQGwExMZchib/AQ "R ‚Äì Try It Online")
* Read `x` from stdin.
* Do a full search over all possible nonempty subsets.
* Compare sum of a subset with `m` (initially `m=0`).
* Store maximum value in `m`.
* Print value of `m`.
# Other unsuccessful ideas
## 58 bytes
```
Reduce(max,lapply(lapply(seq(x<-scan()),tail,x=x),cumsum))
```
## 63 bytes
```
Reduce(max,lapply(seq(x<-scan()),function(i)cumsum(tail(x,i))))
```
## 72 bytes
```
m=matrix(x<-scan(),n<-length(x),n);max(apply(m*lower.tri(m,T),2,cumsum))
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 4 bytes
-2 bytes thanks to Adnan
```
√é≈íOM
```
[Try it online!](https://tio.run/##MzBNTDJM/f//cN/RSf6@//9H6xrqKOgaAbFxLAA "05AB1E ‚Äì Try It Online")
[Answer]
# Mathematica, 24 bytes
```
Max[Tr/@Subsequences@#]&
```
[Answer]
# [Haskell](https://www.haskell.org/), 41 33 bytes
```
import Data.List
g=maximum.concatMap(map sum.inits).tails
```
```
maximum.(scanl(+)0=<<).scanr(:)[]
```
[Try it online!](https://tio.run/##FcxLDoIwGEXhuau4IQ4gpoQCvggswDUggz9QC7GtTYuP1Vvr4AzO5JvJ34VSQXbXoOmz6KfOUz@SUekuK7q2zfL/ubTJ@iG8yKGDJgsnaMIW74ebPBKOEhVYDbbHAUewE87gBRjnsTJWgdcJmgb9xaxCCjdsNC0matYtZo2URNTDd7wpkj6w0dof "Haskell ‚Äì Try It Online") thanks to Laikoni
[Answer]
# [Gaia](https://github.com/splcurran/Gaia), 6 bytes
```
0+·∏ãŒ£¬¶‚åâ
```
[Try it online!](https://tio.run/##S0/MTPz/30D74Y7uc4sPLXvU0/k/9VHbxP/RuoYKukYKusaxAA "Gaia ‚Äì Try It Online")
[Answer]
# [k](https://en.wikipedia.org/wiki/K_(programming_language)), 14 bytes
```
|/,/+\'(1_)\0,
```
[Try it online!](https://tio.run/##y9bNz/7/P82qRl9HXztGXcMwXjPGQOd/moOelYFVwn9DBSMFYwVdEwVdUwUzBXMFXQsFSwVDAwVdQ0MgNgJiYwVDEwA "K (oK) ‚Äì Try It Online")
```
            0, /prepend a zero (in case we're given all negatives)
       (1_)\   /repeatedly remove the first element, saving each result
    +\'        /cumulative sum over each result, saving each result
  ,/           /flatten (fold concat)
|/             /maximum (fold max)
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 11 bytes
```
¬£√£Y mx√Éc rw
```
[Try it online!](http://ethproductions.github.io/japt/?v=1.4.5&code=o+NZIG14w2Mgcnc=&input=WzEsIDIsIDMsIC00LCAtNSwgNiwgNywgLTgsIDksIDEwLCAtMTEsIC0xMiwgLTEzLCAxNF0=)
## Explanation
```
¬£√£Y mx√Éc rw
m@√£Y mx} c rw   // Ungolfed
m@     }        // Map the input array by the following function, with Y=index
  √£Y            //   Get all subsections in input array length Y
     mx         //   Sum each subsection
         c rw   // Flatten and get max
```
## Alternate method, 11 bytes
From @ETHproductions; based on [Brute Forces' Husk answer](https://codegolf.stackexchange.com/a/138700/69583).
```
¬£sY √•+√Éc rw
```
Gets all tails of the input array and cumulatively sums each. Then flattens the array and gets the max.
[Try it online!](http://ethproductions.github.io/japt/?v=1.4.5&code=o3NZIOUrw2Mgcnc=&input=WzEsIDIsIDMsIC00LCAtNSwgNiwgNywgLTgsIDksIDEwLCAtMTEsIC0xMiwgLTEzLCAxNF0=)
[Answer]
# Ruby, ~~61~~ ~~59~~ 57 bytes
I just started learning Ruby, so this is what I came up with.
```
s=0
p [gets.split.map{|i|s=[j=i.to_i,s+j].max}.max,0].max
```
I first saw this algorithm at the Finnish version of [this unfinished book](https://cses.fi/book.pdf).
It is very well explained at the page 23.
[Try it online!](https://tio.run/##KypNqvz/v9jWgKtAITo9taRYr7ggJ7NELzexoLoms6bYNjrLNlOvJD8@U6dYOysWKF5RCyJ0DMDs//8NFYwUjBV0TRR0TRXMFMwVdC0ULBUMDRR0DQ2B2AiIjRUMTQA)
[Answer]
# JavaScript, 58 bytes
```
m=Math.max;x=y=>eval("a=b=0;for(k of y)b=m(a=m(a+k,k),b)")
```
Golfed JS implementation of Kadane's algorithm. Made as short as possible.
Open to constructive suggestions!
**What I learnt from this post:** return value of `eval` - when its last statment is a `for` loop - is basically the last value present *inside* the loop. Cool!
**EDIT:** saved four bytes thanks to Justin's and Hermann's suggestions.
[Answer]
# J, 12 bytes
```
[:>./@,+/\\.
```
Similar to zgrep's K solution: the scan sum of all suffixes (produces a matrix), raze, take max
[Try it online!](https://tio.run/##LYxBCsIwFET3OcXDTRFL2p/EqoGI97DwF2IQNy669uwxqV0MDDNv5l3yQoqMVJV7vNrh1h@GebZlb3aWLqfY0fON5MWY5@P1YYJERh2CegIqrP5I@BMurESLPRpaM3FCz1yQsfIVFlflkW0ybqfSftWXHw "J ‚Äì Try It Online")
### NOTE
for not too many more bytes, you can get an efficient solution (19 bytes golfed):
```
[: >./ [: ({: - <./)\ +/\
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-h`](https://codegolf.meta.stackexchange.com/a/14339/), 4 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
√£x √±
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LWg&code=43gg8Q&input=WzEgMiAzIC00IC01IDYgNyAtOCA5IDEwIC0xMSAtMTIgLTEzIDE0XQ)
```
√£x √±     :Implicit input of array
√£        :Sub-arrays
 x       :Reduced by addition
   √±     :Sort
         :Implicit output of last element
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~52~~ 51 bytes
```
f=lambda l:len(l)and max(sum(l),f(l[1:]),f(l[:-1]))
```
[Try it online!](https://tio.run/##PYzNCsIwEITvPkWPG5gFN6l/AZ@k9BCpQWETi1bQp48rgoeBGT6@md/L5VZ9a/moqZym1GnUcyV1qU5dSS96PIstZNJB4vgrkWV0rs33a10o0yDwCOAevMEWO/AeB8gaLGLxlgDpTVn9FTZiIHx/Pg "Python 2 ‚Äì Try It Online")
[Answer]
# Common Lisp, 73 bytes
```
(lambda(a &aux(h 0)(s 0))(dolist(x a s)(setf h(max x(+ h x))s(max s h))))
```
[Try it online!](https://tio.run/##HYpRCsIwEESvMl86QQLdtmo9ztYqKbRaTIS9fVw78AZmePdlzlvl9plfBaxcdB0npeKgX2NCE5i9Aqe3m4UGRfbvUZ5IXNVgPCHBQsj7zEjBU48UtOgQe8QzLrgiDrhBGkQRp3U6SP93fw)
[Answer]
# APL, ~~31~~ ~~29~~ 27 bytes
```
‚åà/‚àä‚àò.{+/W[X/‚ç®‚ç∫‚â§X‚Üê‚ç≥‚çµ]}‚ç®‚ç≥‚ç¥W‚Üê‚éï
```
[Try it online!](http://tryapl.org/?a=%7B%u2308/%u220A%u2218.%7B+/W%5BX/%u2368%u237A%u2264X%u2190%u2373%u2375%5D%7D%u2368%u2373%u2374W%u2190%u2375%7D1%202%203%20%AF4%20%AF5%206%207%20%AF8%209%2010%20%AF11%20%AF12%20%AF13%2014&run) (modified so it will run on TryAPL)
## How?
* `‚àä‚àò.{+/W[X/‚ç®‚ç∫‚â§X‚Üê‚ç≥‚çµ]}‚ç®‚ç≥‚ç¥W‚Üê‚éï` Generate sums of subvectors
* `‚åà/` Maximum
[Answer]
# CJam, 24 bytes
```
q~:A,{)Aew{:+}%}%e_0+:e>
```
Function that takes array of numbers as input.
[Try it Online](http://cjam.aditsu.net/#code=q~%3AA%2C%7B)Aew%7B%3A%2B%7D%25%7D%25e_0%2B%3Ae%3E&input=%5B1%202%203%20-4%20-5%206%207%20-8%209%2010%20-11%20-12%20-13%2014%5D)
```
q~:A   e# Store array in 'A' variable
,{)Aew e# Get every possible sub-array of the array
{:+}%  e# Sum every sub array
}e_    e# flatten array of sums
0+     e# Add zero to the array
:e>    e# Return max value in array
```
[Answer]
# [MY](https://bitbucket.org/zacharyjtaylor/my-language/src/031637f7e827bde8c06b68ddaf39d9773476667a?at=master), 11 bytes
```
‚éïùüö35«µ'∆í‚áπ(‚çê‚Üµ
```
[Try it online!](https://tio.run/##y638//9R39QPc@fPMjY9vlX92KRH7Ts1HvVOeNS29f//aEMdIx1jHV0THV1THTMdcx1dCx1LHUMDHV1DQyA2AmJjHUOTWAA) MY is on TIO now! Woohoo!
## How?
* `‚éï` = evaluated input
* `ùüö` = subvectors
* `35«µ'` = `chr(0x53)` (Œ£, sum)
* `∆í` = string as a MY function
* `‚áπ` = map
* `(` = apply
* `‚çê` = maximum
* `‚Üµ` = output with a newline.
Sum was fixed (`0` on empty arrays) in order for this to work. Product was also fixed.
[Answer]
# Axiom, 127 bytes
```
f(a:List INT):Complex INT==(n:=#a;n=0=>%i;r:=a.1;for i in 1..n repeat for j in i..n repeat(b:=reduce(+,a(i..j));b>r=>(r:=b));r)
```
This would be O(#a^3) Algo; I copy it from the C++ one... results
```
(3) -> f([1,2,3,-4,-5,6,7,-8,9,10,-11,-12,-13,14])
   (3)  24
                                                    Type: Complex Integer
(4) -> f([])
   (4)  %i
                                                    Type: Complex Integer
(5) -> f([-1,-2,3])
   (5)  3
                                                    Type: Complex Integer
```
[Answer]
# Scala, 105 bytes
```
val l=readLine.split(" ").map(_.toInt);print({for{b<-l.indices;a<-0 to b+2}yield l.slice(a,b+1).sum}.max)
```
I didn't find any better way to generate the sublists arrays.
[Answer]
# Java 8, 242 bytes
```
import java.util.*;v->{List a=new Stack();for(String x:new Scanner(System.in).nextLine().split(" "))a.add(new Long(x));int r=0,l=a.size(),i=l,j,k,s;for(;i-->0;)for(j=l;--j>1;r=s>r?s:r)for(s=0,k=i;k<j;)s+=(long)a.get(k++);System.out.print(r);}
```
[Try it here.](https://tio.run/##LVBdTwIxEHznV0x8auVaOMUvavEPoDEx8cX4UI@CvSu9S7uHqOG3Y0GSTrqdZndnpjYbI9rOhnrR7N26ayOhzpzsyXl5rgCMRng2mW6XoE@Lj2@yomr7QINB5U1KeDQu/A4AF8jGpaksng5PYNO6BSr2erg2XGVul5FPIkOuwhMCNPYbMfudu0QwOtgvvJCpGsbVso3shaILK2ynx4/KhGAz@Z3IrqULXAa7pbkLlnGZOu@IneGMcyPNYsEOLfM2rNiWc5W1Iepx4bWRyf3khsJpX9RFU6TjJuWEmI0VP9S19kqIelaqqNMsPqRpPPIpD2i0U819rXgaaubz@LxtZYk1wyFXJ2VtT7LLwolFrnZ79W@66z98Nn3yfsxmnZM7eXx7h@H/sQVZsdB7f0psty9xgUuICcQVrnEDcYs7lGOIssy4yLhEOfkD)
# 106 bytes without using STDIN/STDOUT requirement.. >.>
```
a->{int r=0,l=a.length,i=l,j,k,s;for(;i-->0;)for(j=l;--j>1;r=s>r?s:r)for(s=0,k=i;k<j;s+=a[k++]);return r;}
```
[Try it here.](https://tio.run/##jU/LboMwELznK/YIYo0wkFdd0y9oLjlGObjESQ3ERLaTqor4droh@YBKHss769nZadRNsf6ibXNox7pT3sOnMvY@AzA2aHdUtYbNo5wIqCO6d3tQsSBuINDxQQVTwwYsSBgVq@6Pr05m2EmVdtqewjca2WGDLXpx7F0kDGNVJuLHu5GdYKypuHDSV@7Dv7mJ9zSglUa0743wiVS7Nkn2sXA6XJ0FJ4ZRPP0v16@O/F9r3HpzgDOFiLbBGXuatn0m2P76oM9pfw3phVqhs5FN68jqH5hi3TnmWCArkc1xgUtkK1wjz5BxTsgJBfJyiKf0/5jHSEaq4qUYZsPIIYcCWAlsDgtYAlvBGngGZEHICQXw8g8)
**Explanation:**
```
import java.util.*;      // Required import for List, Stack and Scanner
v->{                     // Method with empty unused parameter and no return-type
  List a=new Stack();    //  Create a List
  for(String x:new Scanner(System.in).nextLine().split(" "))
                         //  Loop (1) over the STDIN split by spaces as Strings
    a.add(new Long(x));  //   Add the String converted to a number to the List
                         //  End of loop (1) (implicit / single-line body)
  int r=0,               //  Result-integer
      l=a.size(),        //  Size of the List
      i=l,j,k,           //  Index-integers
      s;                 //  Temp sum-integer
  for(;i-->0;)           //  Loop (2) from `l` down to 0 (inclusive)
    for(j=l;--j>1;       //   Inner loop (3) from `l-1` down to 1 (inclusive)
        r=               //     After every iteration: change `r` to:
          s>r?           //      If the temp-sum is larger than the current `r`:
           s             //       Set `r` to the temp-sum
          :              //      Else:
           r)            //       Leave `r` the same
      for(s=0,           //    Reset the temp-sum to 0
          k=i;k<j;)      //    Inner loop (4) from `i` to `j` (exclusive)
        s+=(long)a.get(k++);
                         //     Add the number at index `k` in the List to this temp-sum
                         //    End of inner loop (4) (implicit / single-line body)
                         //   End of inner loop (3) (implicit / single-line body)
                         //  End of loop (2) (implicit / single-line body)
  System.out.print(r);   //  Print the result to STDOUT
}                        // End of method
```
]