[Question]
  [
> 
> This is a overhaul of [this now deleted question](https://codegolf.stackexchange.com/q/152430/56656) by [ar kang](https://codegolf.stackexchange.com/users/77282/ar-kang). If the OP of that question would like to reclaim this question or has a problem with me posting this I'd be happy to accommodate
> 
> 
> 
Given a list of integers as input find the maximum possible sum of a continuous sublist that starts and ends with the same value. The sublists must be of length at least 2. For example for the list 
```
[1, 2, -2, 4, 1, 4]
```
There are 2 different continuous sublists start and end with the same value
```
[1,2,-2,4,1] -> 6
[4,1,4]      -> 9
```
The bigger sum is 9 so you output 9.
You may assume every input contains at least 1 duplicate.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so answers will be scored in bytes with fewer bytes being better.
## Test cases
```
[1,2,-2,4,1,4]  -> 9
[1,2,1,2]       -> 5
[-1,-2,-1,-2]   -> -4
[1,1,1,8,-1,8]  -> 15
[1,1,1,-1,6,-1] -> 4
[2,8,2,-3,2]    -> 12
[1,1,80]        -> 2
[2,8,2,3,2]     -> 17
```
  
[Answer]
# [Haskell](https://www.haskell.org/), 62 bytes
`f` takes a list of integers and returns an integer.
```
f l=maximum[x+sum m-sum n|x:m<-t l,y:n<-t m,x==y]
t=scanr(:)[]
```
[Try it online!](https://tio.run/##PYrBCoMwEETvfkUoPRiSSLVSRBp/xHoIVlG6mxYTaYT@e5oILcsM82ZnUuYxAHhUs5aztsOientcNcx6MBmqV3oD0QBjByIacmDMTM93OqbLoO4EKKXZvvQjAYnKzbhi65hZkaCIrj@uxquwBPhW6xiQOym3LrHS9EovaU3bzvs25wUXBS95zssu2TEoJJHHfve9j1dFrv4Y4BIscBFeYXzmv2116pIv "Haskell – Try It Online")
# How it works
* `t` is the standard "get all suffixes of a list without importing `Data.List.tails`" function.
* In `f l`, the list comprehension iterates through all the non-empty suffixes of the argument list `l`, with first element `x` and remainder `m`.
* For each, it does the same for all nonempty suffixes of `m`, selecting first element `y` and remainder `n`.
* If `x` and `y` are equal, the list comprehension includes the sum of the elements between them. This sublist is the same as `x:m` with its suffix `n` stripped off, so the sum can be calculated as `x+sum m-sum n`.
[Answer]
# JavaScript (ES6), ~~68~~ 62 bytes
```
a=>a.map(m=(x,i)=>a.map((y,j)=>m=j<=i||(x+=y)<m|y-a[i]?m:x))|m
```
### Test cases
```
let f =
a=>a.map(m=(x,i)=>a.map((y,j)=>m=j<=i||(x+=y)<m|y-a[i]?m:x))|m
console.log(f([1,2,-2,4,1,4] )) // -> 9
console.log(f([1,2,1,2]      )) // -> 5
console.log(f([-1,-2,-1,-2]  )) // -> -4
console.log(f([1,1,1,8,-1,8] )) // -> 15
console.log(f([1,1,1,-1,6,-1])) // -> 4
console.log(f([2,8,2,-3,2]   )) // -> 12
console.log(f([1,1,80]       )) // -> 2
```
### Commented
```
a =>                    // a = input array
  a.map(m =             // initialize m to a function (gives NaN in arithmetic operations)
    (x, i) =>           // for each entry x at position i in a:
    a.map((y, j) =>     //   for each entry y at position j in a:
      m =               //     update m:
        j <= i ||       //       if j is not after i
        (x += y) < m |  //       or the sum x, once updated, is less than m
        y - a[i] ?      //       or the current entry is not equal to the reference entry:
          m             //         let m unchanged
        :               //       else:
          x             //         update m to the current sum
    )                   //   end of inner map()
  ) | m                 // end of outer map(); return m
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 12 bytes
```
ĠŒc€Ẏr/€ịḅ1Ṁ
```
[Try it online!](https://tio.run/##y0rNyan8///IgqOTkh81rXm4q69IH0Tv7n64o9Xw4c6G/4fbgfzI//@jow11jHR0jXRMdAx1TGJ1uBTAAkAMZusaguTAJFQOBC1AIhZIAkCuGZAAixgBpYFajHUQOiwMYmMB "Jelly – Try It Online")
### How it works
```
ĠŒc€Ẏr/€ịḅ1Ṁ  Main link. Argument: A (array)
Ġ             Group the indices of A by their corresponding values.
 Œc€          Take all 2-combinations of grouped indices.
    Ẏ         Dumps all pairs into a single array.
     r/€      Reduce each pair by range, mapping [i, j] to [i, ..., j].
        ị     Index into A.
         ḅ1   Convert each resulting vector from base 1 to integer, effectively
              summing its coordinates.
           Ṁ  Take the maximum.
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 10 bytes
```
▲mΣfΓ~€;ṫQ
```
[Try it online!](https://tio.run/##yygtzv6vkKtR/KipsUjz0Lb/j6Ztyj23OO3c5LpHTWusH@5cHfj///9oQx0jHV0jHRMdQx2TWC4wF4iBLF1DkDiYBIuDoAWIbwHnAjlmQALINwJKARUb68DUWhjARUGCAA "Husk – Try It Online")
## Explanation
```
▲mΣfΓ~€;ṫQ  Input is a list, say x=[1,2,-2,4,1,4]
         Q  Slices: [[1],[2],[1,2],..,[1,2,-2,4,1,4]]
   f        Keep those that satisfy this:
    Γ        Deconstruct into head and tail, for example h=2 and t=[-2,4,1]
        ;    Wrap h: [2]
      ~€     Is it an element of
         ṫ   Tails of t: [[-2,4,1],[4,1],[1]]
            Result: [[1,2,-2,4,1],[4,1,4]]
 mΣ         Map sum: [6,9]
▲           Maximum: 9
```
[Answer]
# [Haskell](https://www.haskell.org/), 66 bytes
```
maximum.f
f(x:y)=[sum$x:take a y|(a,b)<-zip[1..]y,b==x]++f y
f x=x
```
[Try it online!](https://tio.run/##HcyxDoMgEADQ3a@4wUEjkNjR9L4EGU4jlAiGFE2Opt9e2nR543tQ3rcQqsO5RmIfr6hsYzueSo86X7Hl6aR9A4Ly7kgs/V2@fNKjUqaIBZHNMFgojQVG/hX@AIT09McJLTjQchTyJv6a@lltIJerXFP6Ag "Haskell – Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~108~~ ~~103~~ ~~90~~ ~~88~~ 83 bytes
```
function(l)max(combn(seq(l),2,function(x)"if"(rev(p<-l[x[1]:x[2]])-p,-Inf,sum(p))))
```
[Try it online!](https://tio.run/##PYtBCoAgEEWvEq1GmFlkm4gu0BnERYkDQppphbc3V/3de4@fKncLVX6Cud0Z4BB@K2BOvwfI9mqMEv9aRO@4h2RfiAsdqqhBz0VJrQVFpDUw5sdDFG2VwYDEqf1pRNnEBw "R – Try It Online")
`combn` strikes again! Generates all sublists of length at least `2`, sets the sublist sum to `-Inf` if the first and last are not equal, and takes the max of all the sums.
The `"if"` will raise a bunch of warnings but they are safely ignorable -- that's probably the best golfing trick here, `rev(p)-p` is zero in the first element iff `p[1]==tail(p,1)`, and `"if"` uses the first element of its condition with a warning.
[Answer]
# [Python 3](https://docs.python.org/3/), 81 bytes
```
lambda x,e=enumerate:max(sum(x[i:j+1])for i,a in e(x)for j,b in e(x)if(a==b)*j>i)
```
[Try it online!](https://tio.run/##TY7NCoMwEITP5ilyTNoVGpUiQvoimkOkCY00UfyB9OnTRBFk2YFvZgd2@q2f0ZVBY4678JW2f0vsQXHlNqtmuarGSk@WzRLfmma4M0H1OGMDEhuHFfE7DtCfaDSRnPf0NrwMDSn0KepQ1rYMCsgLqIBBJQBleHfiHpCzlO56pmnqZNVXJ/IzymEV8SC2SriU6ocQDULZNBu3Eh3fouEP "Python 3 – Try It Online")
[Answer]
# [Python](https://docs.python.org/2/), 62 bytes
```
f=lambda l:l and max(f(l[1:]),[sum(l)]*(l.pop()in l[:1]),f(l))
```
[Try it online!](https://tio.run/##TY7bCsIwDECf7VfksZVMbJ0yB35JCVIvw0HXla0D/fqZOYcSkpCTQ0h8pUcbzDhWJ@@ay82BLz24cIPGPWUlvdUlKbT90EivaC39JrZRqjqAt6XmFTtKjenep/PV9fceTmDFylqNBjODOWrMCY@EC@Qk3M9zpifnUwmzfJGmKCZcEOr9P2V24EL4dQ17fGA33dTmZxZbYiRIiKrtoA5xSAjtkLjzBL93S7GKXR0SVPJjKZyt8Q0 "Python 2 – Try It Online")
Outputs a [singleton list](https://codegolf.meta.stackexchange.com/a/11884/20260).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~13~~, 12 bytes
```
=ṚṖḢ
ẆÇÐfS€Ṁ
```
[Try it online!](https://tio.run/##y0rNyan8/9/24c5ZD3dOe7hjEdfDXW2H2w9PSAt@1LTm4c6G/4fbj056uHPG///RhjoKRjoKukBsoqMA5JjEAgA "Jelly – Try It Online")
One byte saved by Mr. Xcoder, who is currently competing with me. :D
Explanation:
```
        # Helper link:
=Ṛ      # Compare each element of the list to the element on the opposite side (comparing the first and last)
  Ṗ     # Pop the last element of the resulting list (so that single elements return falsy)
   Ḣ    # Return the first element of this list (1 if the first and last are equal, 0 otherwise)
        # Main link:
Ẇ       # Return every sublist
 Ç      # Where the helper link
  Ðf    # Returns true (1)
    S€  # Sum each resulting list
      Ṁ # Return the max
```
[Answer]
## [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 54 bytes
```
Max@SequenceCases[#,{a_,b___,a_}:>2a+b,Overlaps->All]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78H1CUmVei4KCloPHfN7HCITi1sDQ1LznVObE4tThaWac6MV4nKT4@XicxvtbKzihRO0nHvyy1KCexoFjXzjEnJ1btv6aCvoNCNZeCQrWhjoKRjoIuEJvoKAA5JrU6CGEQCeHrGkIUQWi4GgiygIhboAmDxMxAFETcCKwQZIYx3FiofoNartr/AA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# Pyth, 15 bytes
```
eSsMf&qhTeTtT.:
```
[Try it online](http://pyth.herokuapp.com/?code=eSsMf%26qhTeTtT.%3A&input=%5B1%2C%202%2C%20-2%2C%204%2C%201%2C%204%5D&debug=0)
### Explanation
```
eSsMf&qhTeTtT.:
             .:Q  Take all sublists of the (implicit) input.
    f qhTeT       Take the ones that start and end with the same number...
     &     tT     ... and have length at least 2.
  sM              Take the sum of each.
eS                Get the largest.
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
ŒʒćsθQ}OZ
```
[Try it online!](https://tio.run/##MzBNTDJM/f//6KRTk460F5/bEVjrH/X/f7SuoY6ukQ6YjAUA "05AB1E – Try It Online")
**Explanation**
```
Œ          # push sublists of input
 ʒ    }    # filter, keep values where
  ć        # the head of the list, extracted
     Q     # is equal to
   sθ      # the last element of the rest of the list
       O   # sum the resulting sublists
        Z  # get the max
```
[Answer]
# [Clean](https://clean.cs.ru.nl), ~~94~~ ~~90~~ 86 bytes
```
import StdEnv,StdLib
@l=last(sort[sum(l%(i,j))\\e<-l&i<-[0..],j<-elemIndices e l|j>i])
```
[Try it online!](https://tio.run/##JY2xCoMwFEX3fkWWFoVEdOsQi4MdBDdHzZDG1/LCSyxNLBT67U0Fp8vhwLmGQPvklnklYE6jT@ieyyuyIc5X/@bb9Hg7NFSTDjELmxrD6jI6Zshtnk8TSEEnlGIsi0JxKwUQuM7PaCAwYPS1F1R5GqLeqjVr2Fjxip9LlX7mTvoRkuj61H68dmh22E// "Clean – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 86 bytes
*[Outgolfed by Dennis](https://codegolf.stackexchange.com/a/152470/60919)*
```
lambda x:max(sum(x[i:j+1])for i,v in enumerate(x)for j in range(i+1,len(x))if v==x[j])
```
[Try it online!](https://tio.run/##TY/RisMgEEWf61fMo6ETWG26pIF8iTsUu2t2DYkJiSn261NNW7qIM3g8XGbGm/8bnFyb@mvtdH/50RCqXgc@Lz0PylbtXlDWDBNYvIJ1YNzSm0l7w8OG2wQn7X4Nt3uBnXHxI7MNXOs6qJay1ZvZn7/1bGaoQbGdUgIl5hILFFgQnghfMF7C4@Odi@RslTAvXlI6ZcIloTj@p5F9xkL4dGX0YsAhZQr5NssPiogRY9tablw8wrD42NMu73Erthsn6zw0fLMyfFjrHQ "Python 2 – Try It Online")
Generates all sublists larger than length 2, where the first element is equal to the last, then maps each to its sum and selects the largest value.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 64 bytes
```
->l{w,*r=0;(z=l.index w)&&r<<w+l[z..-1].sum while w=l.pop;r.max}
```
[Try it online!](https://tio.run/##PY3bCoMwEETf/Yo8SS@bYKyUgNofCXloqVIhtiFWYr18e7oJtCw7zJkdWDvePr6tPb3oxcHB1lm5m2vNuue9mYjbp6mtKnfUcmaMcsWGsSfu0emGOGyZlykt66/T5qXkkAPNoQAOhYKIuOgoD3nUmIcRgcUfEc4oyDmesHyCX1dkSuELs6x6Tcz4HkgrtUo2/wU "Ruby – Try It Online")
[Answer]
# [Julia 0.6](http://julialang.org/), 70 bytes
```
a->maximum(sum(a[i:k]) for b=[findin(a,x) for x=a] for i=b,k=b if k>i)
```
[Try it online!](https://tio.run/##XZDdCsIwDEbv9xTBqw06sXXqFCriM3g3dtFpK3Gugt1gbz/TFX8mJSHkcPJBb90d1XowIKHSV7SDSveN6rHpmthRqQJ3dZmAeTyhkoVBe0EbK9aHVS9VOQ4oK1bLCtBAvcdk0PYSdQ7tFY7K6flJuzY6tNSdbmHmBzgTcLMQGxiYuOBMsFSwjHGWUa6UsJ1CqrBfffcp987YA0uzX8m/3OM8UL76p8TW1AL@cQV5dHj5zgQupmq@CED8Ox@FbyL6jOEF "Julia 0.6 – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 bytes
Uses some features that post-date the challenge.
```
Ẇµ.ịEȧḊµƇ§Ṁ
```
[Try it online!](https://tio.run/##y0rNyan8///hrrZDW/Ue7u52PbH84Y6uQ1uPtR9a/nBnw//D7Y@a1kT@/x8dbahjpKNrpGOiY6hjEqvDpQAWAGIwW9cQJAcmoXIgaAESsUASAHLNgARYxAgoDdRirIPQYWEQGwsA "Jelly – Try It Online")
### How it works?
```
Ẇµ.ịEȧḊµƇ§Ṁ  || Full program. Takes input from CLA, outputs to STDOUT.
Ẇ            || Sublists.
 µ     µƇ    || Filter-Keep those
    ȧḊ       || ... Which have length at least 2 and ...
 .ị          || ... The elements at floor(0.5) and ceil(0.5) (modular, 1-indexed) ...
    E        || ... Are equal.
         §   || Sum each.
          Ṁ  || Maximum.
```
-1 with help from [caird](https://codegolf.stackexchange.com/users/66833/caird-coinheringaahing).
[Answer]
## Batch, 179 bytes
```
@set s=%*
@set/a"m=-1<<30
:l
@set/at=n=%s: =,%
@set s=%s:* =%
@for %%e in (%s%)do @set/at+=%%e&if %%e==%n% set/a"m+=(m-t)*(m-t>>31)
@if not "%s%"=="%s: =%" goto l
@echo %m%
```
Takes input as command-line parameters.
[Answer]
# C, 104 bytes
```
i,j,s,l;f(a,n)int*a;{for(i=0,l=1<<31;i<n;++i)for(s=a[j=i];++j<n;l=a[j]-a[i]?l:s>l?s:l)s+=a[j];return l;}
```
[Try it online!](https://tio.run/##fZDRasQgEEXf8xWyUNBmAqsx2bDG7oekeZB0UwzWlpg@hXx7qlnKtmxQcWAucw937LL3rltXDQM4MKLHCizRdnpWYu4/R6zlEYykdZ1ToWsr0lSToDupmkHq1guDl01o20w1ur2Ys3sxF3c2xKWbLMbr9D1aZMSyejT6UNpikswJ8icI09VNtGmRRDMFBhkDDhT4IraJr9HP9Pjw9PZqD4B6vI0DKgkRyT8GuzP8i9kZIP5gz2/2jIYEW40h8j0E/00QbhUgVYzB97Yo/jI8ofQlBin2IOUNwnwIv0se/40SUPFAON1jVMeY@wQoD@5l/QE)
# [C (gcc)](https://gcc.gnu.org/), 99 bytes
```
i,j,s,l;f(a,n)int*a;{for(i=0,l=1<<31;i<n;++i)for(s=a[j=i];++j<n;l=a[j]-a[i]?l:s>l?s:l)s+=a[j];l=l;}
```
[Try it online!](https://tio.run/##fZDRasQgEEXf8xWyUNBmAms02bDG7oekeZC0KQabljVvId@ejlnKtmxQccDr3MMdu@yj69bVwgAenOqpgZHZcXo2au6/rtTqIzjN61pwZetRpallQffaNIO2LQoDyi5c28w0tr24s39xF392zKebjK9OLStSyaexI2XJnBBcQZje/cSblmgyc8ghy0ECB7moreP7ij09PTy9vY4HID3d2oGUjKnkHyO/M/DE7DkQ@WAXN3vGQ4KtxhBiDyF/E4RdBUgVY8i9KYq/DCSUWGKQYg9S3iA5hsBZRPw3SiDFA@F0j1EdY@4TEBHcy/oD)
[Answer]
## Clojure, 92 bytes
```
#(apply max(for[i(range(count %))j(range i):when(=(% i)(% j))](apply +(subvec % j(inc i)))))
```
[Answer]
## Java 8, 129 byes
```
a->a.stream().map(b->a.subList(a.indexOf(b),a.lastIndexOf(b)+1).stream().mapToLong(Long::intValue).sum()).reduce(Long::max).get()
```
For each integer `X` in the list, the function finds the sum of the largest sublist with start and end `X`. Then, it finds the maximum sum as the OP specifies.
[Answer]
# Perl, ~~61~~ 59 bytes
Includes `+3` for `-p`:
`max_ident_run.pl`:
```
#!/usr/bin/perl -p
s:\S+:$%=$&;($%+=$_)<($\//$%)||$_-$&or$\=$%for<$' >:eg}{
```
Run as:
```
max_ident_run.pl <<< "1 2 -2 4 1 4 1"
```
]