[Question]
  [
#### Story
My local pizza delivery introduced new discount. You get 50% discount from every second item on your order.
But being greedy capitalists, they forgot to mention that they will rearrange items the way they need to give you as little as possible.
#### Example
Imagine you ordered
```
- Pizza $20
- Pizza $20
- Coke $2
- Coke $2
```
You expect to get $10 discount from the second pizza and $1 from the coke, but they rearrange it as
```
- Pizza $20
- Coke $2
- Pizza $20
- Coke $2
```
and give you $2.
#### Trick
Later I noticed that I can place as many orders as I want simultaneously, so I just split my order into two:
```
 1. 
 - Pizza $20
 - Pizza $20
 2. 
 - Coke $2
 - Coke $2
```
and I got the discount I deserve.
#### Problem
Can you please help me to write a program that calculate the maximum discount I can get by splitting my order.
It should accept a list of prices and return an amount.
For simplicity, all prices are even numbers, so the result is always an integer. Order is never empty. Input have no specific order.
This is code golf, do all usual rules applies.
#### Testcases
```
[10]             -> 0
[10,20]          -> 5
[10,20,30]       -> 10
[2,2,2,2]        -> 2
[4,10,6,8,2,40]  -> 9
```
  
[Answer]
# [Python 3](https://docs.python.org/3/), 33 bytes
```
lambda a:sum(sorted(a)[-2::-2])/2
```
[Try it online!](https://tio.run/##NYfLCsMgEEXX6VcMWSmMNJ3shORHrAtDIh1oNKhZ9OvNA8qBc@7dfuUTQ1/98K5ft06zA6fzvoocU1lm4aRRpLUiK59UfUzAwAGMoQ4JL1k0r@7Wef/F/pqEN9bqR7MlDkUwtqBGaNELlrIe "Python 3 – Try It Online")
This is my first answer :D
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), ~~11~~ 10 bytes
−1 thanks to Jonah.
Anonymous tacit prefix function.
```
⌊∨+.÷∞2⍴⍨≢
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1FP16OOFdp6h7c/6phn9Kh3y6PeFY86F/1Pe9Q24VFv36Ou5ke9a4DCh9YbP2qb@KhvanCQM5AM8fAM/p@moGNowJWmYGigYASnFYxBTCMIBAA "APL (Dyalog Extended) – Try It Online")
`≢` the tally of item prices
…`⍴⍨` use that to reshape…
 `0∞` the list `[0,infinity]`
…`+.÷` sum the division of the following by that:
 `∨` the item prices sorted into descending order
`⌊` floor (because the infinity is actually just the largest representable float and thus the values are slightly too large)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 6 bytes
```
{RιθO;
```
[Try it online!](https://tio.run/##yy9OTMpM/f@/OujcznM7/K3//482MtABIR2jWAA "05AB1E – Try It Online")
Sort, `R`everse, split into even and odd indices, get second part, sum and take half.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
ṢU0Hƭ€S
```
[Try it online!](https://tio.run/##ASQA2/9qZWxsef//4bmiVTBIxq3igqxT/8OHxZLhuZj//1syMCwxMF0 "Jelly – Try It Online")
Based on [Adám's method](https://codegolf.stackexchange.com/a/233642/66833), so go upvote that as well
## How it works
```
ṢU0Hƭ€S - Main link. Takes a list L on the left
ṢU      - Sort L in descending order
    ƭ€  - Tie the previous 2 atoms, and alternate between the two for each element:
  0     -   At odd-indexed elements: Replace the element with 0
   H    -   At even-indexed elements: Halve the element
      S - Sum
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `s`, 4 bytes
```
sṘy½
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=s&code=s%E1%B9%98y%C2%BD&inputs=%5B20%2C20%2C2%2C2%5D&header=&footer=)
[5 bytes](https://lyxal.pythonanywhere.com?flags=s&code=s%E1%B9%98y%C2%BD&inputs=%5B20%2C20%2C2%2C2%5D&header=&footer=) without the flag. Port of ovs's answer but with two key differences:
* `y`, uninterleave, pushes both halves separately on the stack, so we don't need to get the second half
* We have the `s` flag, so we don't need to sum the list in the code.
```
s    # Sort
 Ṙ   # Reverse
  y  # Interleave
   ½ # Take half (vectorised)
```
[Answer]
# [R](https://www.r-project.org/), 35 bytes
Thanks to att for spotting a bug.
```
function(p).5*p%*%!rank(-p,,"f")%%2
```
[Try it online!](https://tio.run/##K/qfklmcnF@aV2L7P600L7kkMz9Po0BTz1SrQFVLVbEoMS9bQ7dAR0cpTUlTVdUIrlojWcPIQAeEdIw0NbngwoYGSJxkIBeoRhNTSMcYTdRIxwjdKLANhhCl/wE "R – Try It Online")
Takes the dot-product (`%*%`) of `p/2` and a vector of 0s and 1s, with the 1s at the positions corresponding to even ranks in the sorted version of `-p`. We need to use `.5*p` instead of `p/2` because of operator precedence. The `"f"` is needed to handle ties correctly in the vector of ranks.
Dominic van Essen [also has an R answer](https://codegolf.stackexchange.com/a/233656/86301), with a different strategy, currently at 36 bytes.
[Answer]
## [Perl 5](https://www.perl.org/), ~~49~~ ~~44~~ 43 bytes
```
sub f{@_=sort$a-$b,@_;pop;pop()/2+(@_&&&f)}
```
[Try it online!](https://tio.run/##K0gtyjH9X1qcqlBmqmdoYP2/uDRJIa3aId62OL@opFolUVclqdYh3rogvwCENTT1jbQ1HOLV1NTSNGv/FydWKqRpGBpoWnPBmDoKRuhcHQVjhJARUASCEEIGEAEDiOB/AA "Perl 5 – Try It Online")
-6 thanx to @kjetil-s; [previous](https://tio.run/##K0gtyjH9X1qcqlBmqmdoYP2/uDRJIa3aId62OL@opFol0cbWTiWp1iHeuiC/AIQ1NPWNtDUc4tXU0oCkpmbt/@LESoU0DUMDTWsuGFNHwQidq6NgjBAyAopAEELIACJgABH8DwA "Perl 5 – Try It Online").
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 25 bytes
```
-Tr@Sort[-#/2][[2;;;;2]]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7XzekyCE4v6gkWldZ3yg2OtrIGgiMYmPV/gcUZeaVRCvr2qU5KMeq1QUnJ@bVVXNVGxrU6oBIHSMEQ8cYzDbSAUMw0wDEAhK1XLX/AQ "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [R](https://www.r-project.org/), 36 bytes
```
function(p)sum(-sort(-c(p,0))*0:1)/2
```
[Try it online!](https://tio.run/##ZYxLCoAwDAX3niSRFNO4EzxNROjCtvRz/loRRBTeZobhpba5rKH6sra9ei0ueIiY6wEmh1TAKERixJEXi5M8OSgI0zUSxOHRll@gHXuDf0XzxwrJfdVO "R – Try It Online")
Assumes the price of each item in the order is non-negative.
[Answer]
## [Perl 5](https://www.perl.org/), ~~66~~ 64 bytes
```
use List::Util pairmap,sum;sub f{sum+pairmap{$b/2}sort{$b-$a}@_}
```
[Try it online!](https://tio.run/##K0gtyjH9X1qcqlBmqmdoYA1m@mQWl1hZhZZk5igUJGYW5SYW6BSX5loXlyYppFUDWdpQ0WqVJH2j2uL8ohIgS1clsdYhvvZ/cWKlQpqGoYGmNReMqaNghM7VUTBGCBkBRSAIIWQAETCACP4HAA "Perl 5 – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 23 bytes
```
Ｗ⁻θυＦ№ι⌊ι⊞υ⌊ιＩ⊘ΣＥυ×ι﹪κ²
```
[Try it online!](https://tio.run/##VYyxDsIwDER3vsKjI4Wla8cuLJUqwVZ1iNqgWCQ1JHX5/GDoxOmWu3e6Obg8s4u1vgNFD9jTKgVfFsQYuHMG7FjWDcmCIkqSkIyiQUpA@Svb05BJp50rG15c3P2CV0W9e36XN0q@/H54kcj4sNCYQ22t49hoPjxN9bzHDw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｗ⁻θυＦ№ι⌊ι⊞υ⌊ι
```
Sort the input in descending order.
```
Ｉ⊘ΣＥυ×ι﹪κ²
```
Multiply each value by its index modulo 2, then take half the sum.
[Answer]
# JavaScript (ES6), 49 bytes
```
f=a=>1/a.sort((a,b)=>a-b)?0:a.pop(a.pop())/2+f(a)
```
[Try it online!](https://tio.run/##bYzNCsIwEITvPkWOu5jmT7wIqQ8iOWxrI0rphqb09SMk3uoMzOUbvg/tlMf1nbZu4edUSvTke6tJZV43AJID@p66Ae/mRipxgraI2p0jEJaRl8zzpGZ@QYSHNUHUIAqthTkduHT10vj1L5cXExq3B4GTteEncOUL "JavaScript (Node.js) – Try It Online")
### How?
Because `sort()` operates in lexicographical order by default, we unfortunately need the explicit callback function `(a, b) => a - b`, although all test cases would pass without it.
We can stop as soon as the array is empty or only one element remains. Hence the test `1 / a` which evaluates to:
* `Infinity` (truthy) if the array is empty
* A positive float (truthy) if the array is a singleton
* `NaN` (falsy) when at least 2 elements remain
Because the `.pop()` method ignores its argument(s), the expression `a.pop(a.pop())` simply discards the last element and returns the penultimate one.
[Answer]
# [Nibbles](http://golfscript.com/nibbles/index.html), 6 bytes
```
/+`%~>>\`<_~
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m708LzMpKSe1eMGCpaUlaboWa_S1E1Tr7OxiEmzi6yBCUJkFN9UNDbgMDRSMoKSCsQGXkQIYcpkoAIXMFCyAHBMDiHoA)
```
/+`%~>>\`<_~
          _  Input as a list of numbers
        `<   Sort
       \     Reverse
     >>      Remove first item
  `%~        Take every odd-indexed item
 +           Sum
/          ~ Halve
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 7 bytes
```
ṁ½Ċ2Θ↔O
```
[Try it online!](https://tio.run/##yygtzv6f@6ip8f/DnY2H9h7pMjo341HbFP////9HRxvpAKEBEMXqRBtCCBgHJG4MYoLV6BjFxgIA "Husk – Try It Online")
```
      O  # sort in ascending order
     ↔   # reverse
    Θ    # prepend a zero
  Ċ2     # get every 2nd element, starting at the first
ṁ½       # halve each of these, and then sum the results
```
[Answer]
# [PHP](https://php.net/), 68 bytes
FWIW, as small I can go down in PHP.
```
function d($a){rsort($a);for($c=0;$b=$a[++$i]/2;$i++)$c+=$b;echo$c;}
```
[Try it online!](https://tio.run/##dY5NCsIwEIX3PUUps0hIxFqLCGPwCB6gltIm/dskoakr8erGpFuVgcfwfTx4drL@crUhh4eW62x0qgi09Lk4s6zxw8EsBKTIEToBbcUYzPW@QJgZoyCZgA57ORmQ@PKKVIe8pphEkmZ3nWGyMV78w/z4wxR8u29R8lA68XOwZex5/zY2znZ@p1I3hdGNsb1u1nZ04qY/ "PHP – Try It Online")
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), 9 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
êzhrg¥§Σ½
```
[Try it online.](https://tio.run/##y00syUjPz0n7///wqqqMovRDSw8tP7f40N7//w0NuAwNdIygpI6xAZeRDhhymegAhcx0LIAcEwMA)
**Explanation:**
```
ê         # Read the inputs as integer-list
 z        # Sort this list in decreasing order
  h       # Push the length of the list (without popping the list itself)
   r      # Pop and push a list in the range [0, length)
    g     # Filter this list by:
     ¥    #  Modulo-2 (only keep the odd values)
      §   # Use those to index into the decreasing ordered input-list
       Σ  # Sum this list
        ½ # Halve the sum
          # (after which the entire stack - this value - is output implicitly as result)
```
[Answer]
# TI-Basic, 27 bytes
```
Prompt A
SortD(ʟA
sum(ʟAseq(fPart(I/2),I,0,dim(ʟA)-1
```
Output is stored in `Ans` and is displayed at the end.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
~ỤỤ~ḂHḋ
```
[Try it online!](https://tio.run/##y0rNyan8/7/u4e4lQFT3cEeTx8Md3f8Pt2tG/v8fbWgQq6MQbWSgA2EYGugA2cYQQR0FKALxTIAqdMx0LHSMdEwMYgE "Jelly – Try It Online")
Or, more to the point, `ỤỤ^LḂḋH`. Turns out, there's a ton of 7-byters--and when that's the case, it really seems like there ought to be one in 6. I wouldn't normally post this until I've found something that does beat [the existing solution in 7](https://codegolf.stackexchange.com/a/233644/85334), but it's continued to elude me, and this is at least different enough for someone else to maybe springboard off of.
```
 ỤỤ        Get a permutation of [1 .. len(input)] in the same order as
~          the bitwise negation of the input.
   ~Ḃ      Is the bitwise negation of each element odd?
     H     Halve the results,
      ḋ    and take the dot product with the input.
```
Bonus in-between: `ṢUḊm2SH`
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~100~~ ~~78~~ 73 bytes (maybe 74 bytes because of the L"!")
```
r;f(a,l)int*a;{qsort(a,l,4,L"\x72b068bǃ");for(r=0;l--;++a)r+=*++a/2;r=r;}
```
[Try it online!](https://tio.run/##bVLLTsMwELzzFatIlfJwRJu0vEzggLjxB20PwXVKVLcF24eoVU58HH9F2NhxcQVR4mhnvDvjXbN0zVjXSVqFJRFRvdNxSY8fai91D5ApeQkWzXX2Or66ef36DCJa7WUoizEVaUqTpIxkUsT4v8yoLCRtu21Z78IIjheAD9YDzZWezJdQwHEybomJMxcTyByW@xiB3OFTi2cI27clcY8ri5vyQ9Wh0JDXEuPBBMJuVvWB7ytr6dIG8VDAozKfys6o3KfyM2rqUydx3rxzpvnK6uO5ZgTMEYcNMXvjbMOl5bHXz9miuX3CbxYQ8OM8aEm9W/EGm29ScRIQUjAY3DsrTtC5cTGFJDFb3WyG@cSwRWXbUMMvyYkGMVDiLxVr5FgpxJ6FeE8ieqLeJZatwmAeeKAx21@HGtPG6BodC@Mp@q3pJY/UaIUNqB/7LtwFuOp5vSy2uPwjtYT0AXy5XkmhUhVqAiLyfXOET1Oxx/pbcLSCkVrsUFYRcCNSRcGdenvRdt@sEuVademBN5wpXbLNDw "C (gcc) – Try It Online")
Saved a whopping 22 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)!!!
[Answer]
# [PARI/GP](http://pari.math.u-bordeaux.fr/), ~~43~~ 30 bytes
saved 13 bytes thanks to the comment.
adapted from [@Basto's answer](https://codegolf.stackexchange.com/a/233649/110802)
---
30 bytes:
```
a->-[0..#a-1]%2/2*vecsort(-a)~
```
[Try it online!](https://tio.run/##XYzBCsIwEETv/YqlRUhktybpQaSkPxJyCGIkILakwYvor8ek0IsszL4ZhllcDHRfstfv7GgiI/q@cyTtQZ3U8XW7rnNMjBz/5g9A8whrAg3GKIEKq1g0Z5QS5YCywKX4Pyc2KeX941BR4XbWjo2fIwtaInR1H2GJ4ZlYZRMsQgs0QYvg94hzPuYf)
34 bytes:
```
a->[0..#a-1]%2/2*vecsort(-a)~*(-1)
```
[Try it online!](https://tio.run/##XYzBCoMwEETvfsWiFBLZtW48SJH4IyGHUJoSKFWi9FLaX0@N4KUszL4ZhpldDHSfk9fv5Gg0bdNUjtie1FnVr9t1meIqyMlvLYhl@gAUj7CsoMEY1aLCLBZNj8zIHfIGl83/uXaXrXx87DIq3M/aofBTFEEzQpX3EeYYnqvIbIJFKIFGKBH8EUkph/QD)
---
43 bytes:
```
a->sum(i=0,(#a-2)\2,vecsort(a)[#a-1-2*i])/2
```
[Try it online!](https://tio.run/##XY7BCoMwEETvfsWil6RMqFkPRUR/JM0hlFoCbZVoeyn99jQRvJSF2TfDMuzsgle3OY79Jzo1LK@H8H0NUTnF8sx4Xy/LFFbhpEmRVnzwVh45fomKu19W6skYrsHIYmFO0Bq6gU7QJv/n6k3S8b7RZGRsY21XjFNIL2hQlftBc/DPVWQ23oJKUgOVoHGPpJRd/AE)
---
It calculates the sum of alternating elements in the input array `a` after sorting it in decreasing order.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 34 bytes
```
f=->l{*l,a,b=l.sort;b ?a/2+f[l]:0}
```
[Try it online!](https://tio.run/##KypNqvz/P81W1y6nWitHJ1EnyTZHrzi/qMQ6ScE@Ud9IOy06J9bKoPZ/gUJadLShgY6RDoiIjeWCCxjoGBvExv4HAA "Ruby – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-x`](https://codegolf.meta.stackexchange.com/a/14339/), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
+1 byte to handle unsorted inputs.
```
ñÍË*½*Eu
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LXg&code=8c3LKr0qRXU&input=WzIwLDEwLDMwXQ)
[Answer]
# [jq](https://stedolan.github.io/jq/), 37 bytes
```
sort|.[range(length-1;-1;-2)]=0|add/2
```
[Try it online!](https://tio.run/##yyr8/784v6ikRi@6KDEvPVUjJzUvvSRD19AahIw0Y20NahJTUvSN/v@PNjSI5QISOkZwWscYxDTSAUMgy0QHKGymYwHkmhjEAgA "jq – Try It Online")
[Answer]
# Haskell + [hgl](https://gitlab.com/WheatWizard/haskell-golfing-library), 17 bytes
```
mF(//2)<cr<uaL<sr
```
## Explanation
This is a very simple chain of operations:
* `sr` sorts the input.
* `uaL` divides it into two lists, those at even indices and those at odd indices.
* `cr` gets the even indices.
* `mF` sums them together using a custom function ...
* `(//2)` divides by 2.
## Reflections
Ok so there's a lot to reflect on.
The fact that `uaL` exists is basically just dumb luck. I made `uaL` because I needed it to make something else and I just gave it a name because I thought it could come in handy at some point. If it didn't exist this would be a lot longer.
Initially I was going to use `im` which takes a list and a list of indices and produces a list of all the indices. This would have been pretty cumbersome.
We probably need a couple of index / slicing builtins because next time we probably won't be as lucky that something like `uaL` just happens to exist.
Next up is dividing by 2. Arithmetic is something I haven't put a lot of time into so I know it's lacking. However, there should be builtins to divide things by small amounts. `dv2` should exist and would have saved a byte. I also noticed that `fdv` a function that is supposed to be the flip of `dv` is exactly the same as `dv`. This is just a bug.
So there's a lot to work on here.
[Answer]
# [Arturo](https://arturo-lang.io), 35 bytes
```
$=>[/∑map reverse sort&[x,y][y]2]
```
[Try it](http://arturo-lang.io/playground?tldKA4)
Same method as most of the other answers. Sort, reverse, take every other element, sum, and halve.
[Answer]
# [Thunno](https://github.com/Thunno/Thunno), \$ 9 \log\_{256}(96) \approx \$ 7.41 bytes
```
z;ZlAKS2/
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72sJKM0Ly9_wYKlpSVpuhYrq6yjchy9g430IXyo8ILV0UYGOiCkYxQLEQIA)
or [verify all test cases](https://ato.pxeger.com/run?1=m72sJKM0Ly9_abRSilLsOs9qlygfJQVdXTsFpSifpaUlaboWK6uso3IcvYON9CH8RVHeEMaCm8XRhgaxCshAGajTgAsorGOELAMSNoUK6xjDZUDChkDlRjpgGIus3Igr2kQHqMFMxwIoZwLSAxK2hNgNAA)
Port of [ovs's 05AB1E answer](https://codegolf.stackexchange.com/a/233646/114446)
#### Explanation
```
z;ZlAKS2/  # Implicit input
z;         # Reverse-sort
  Zl       # Uninterleave
    AK     # Last item
      S    # Sum
       2/  # Halve
           # Implicit output
```
[Answer]
# Java, 86 bytes
```
L->{int s=0,i=L.size();for(L.sort((a,b)->a<b?1:-1);i-->0;)s+=i%2*L.get(i);return s/2;}
```
[Try it online.](https://tio.run/##ZZBRa8IwFIXf/RUXYZDMNNNOxrC2Y@xp0Pni49hDWqPE1USSWzcn/vYubdXpJBC4J19yTs5SbESwnH1WeSGcgzeh9K4DoDRKOxe5hEk9NgLkZOlpXqIqeKocjl89tZA2gZRGntp3/OZQoMphAhriKg2SXX3TxX2m4pQ79SMJjebGEj8Yi4QIltEgEePsaTAKBjRSQZD0I@p6sboJb1O@kEgUjazE0mpwd2G0r6LaaF1mhTc6@G2MmsHKpydTtEov3j8EbZOjdEgG/SbhaWLhtcDuL7SQNetcGjIPPrBHrw8v2eb@kT7vocnVML4Hzvkx1UZY3@m6xLpIiEHLL/hr99lasb2omPw7dNyhlWJFBOWZ@ZYzQnluikLmeIa2DH9pD4x1HE39LKH0EH@6dShX3JTI1743LPS1E5q2U@/V646g29M8J6fw9PDnffUL)
**Explanation:**
```
L->{                      // Method with Integer-List parameter and Integer return-type
  int s=0,                //  Sum-integer, starting at 0
      i=L.size();         //  Index-integer, starting at the size of the input-List
  for(L.sort(             //  Sort the input-List
        (a,b)->a<b?1:-1); //  in reversed order
      i-->0;)             //  Loop `i` in the range (size,0]:
    s+=                   //   Increase the sum by:
       i%2                //    Check if the current `i` is odd (1 if odd; 0 if even)
       *L.get(i);         //    And multiply that to the current `i`'th value
  return s/2;}            //  After the loop, return halve the sum
```
]