[Question]
  [
*Inspired by [a question](https://stackoverflow.com/q/45788547/2586922) at Stack Overflow*.
Given a non-empty array of integers `x` and a positive integer `n`, compute the **sum** of each **sliding block** of length `n` along the array `x`, **circularly** filling the missing values at the left with values from the right as follows:
* the first block contains the first entry of `x`, preceded by `n-1` circularly shifted entries;
* the second block has the first and second entries of `x`, preceded by `n-2` circularly shifted entries; and so on.
The output array `y` has the same size as `x`. It is possible for `n` to exceed the length of `x`, and then the values of `x` are circularly **reused several times**.
# Examples
### Example 1 (values are reused only once)
```
x = [2, 4, -3, 0, -4]
n = 3
```
give as output
```
y = [-2, 2, 3, 1, -7]
```
where
* `-2` is the sum of the block `[0, -4, 2]` (the first two values come from the circular shifting)
* `2` is the sum of `[-4, 2, 4]` (the first value comes from the circular shifting)
* `3` is the sum of `[2, 4, -3]` (no circular shifting necessary anymore)
* `1` is the sum of `[4, -3, 0]`
* `-7` is the sum of `[-3, 0, -4]`.
### Example 2 (values are reused several times)
```
x = [1, 2]
n = 5
```
give
```
y = [7, 8]
```
where
* `7` is the sum of the block `[1, 2, 1, 2, 1]` (the first four values have been circularly reused)
* `8` is the sum of the block `[2, 1, 2, 1, 2]` (the first three values have been circularly reused)
# Additional rules
* The algorithm should work for arrays of arbitrary size and for arbitrary integer values. It is acceptable if the program is limited by data type or memory restrictions; but positive as well as negative integer values must be handled.
* Input/output can be taken/produced by any [reasonable means](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods).
* [Programs or functions](http://meta.codegolf.stackexchange.com/a/2422/36398) are allowed, in any [programming language](https://codegolf.meta.stackexchange.com/questions/2028/what-are-programming-languages/2073#2073). [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
* Shortest code in bytes wins.
# Test cases
```
x, n, -> y
[2, 4, -3, 0, -4], 3          ->  [-2, 2, 3, 1, -7]
[1, 2], 5                     ->  [7, 8]
[2], 7                        ->  [14]
[-5, 4, 0, 1, 0, -10, -4], 4  ->  [-19, -15, -5, 0, 5, -9, -13]
[-5, 4, 0, 1, 0, -10, -4], 1  ->  [-5, 4, 0, 1, 0, -10, -4]
[-2, -1, 0, 1, 2, 3], 5       ->  [4, 3, 2, 1, 0, 5]
[-10, 0, 10], 4               ->  [-10, 0, 10]
```
  
[Answer]
# MATL, ~~11~~ ~~10~~ ~~9~~ 7 bytes
*3 Bytes saved thanks to @Luis!*
```
:gyn&Z+
```
The first input is the size of the window and the second input is the array
Try it at [MATL Online](https://matl.io/?code=%3Agyn%26Z%2B&inputs=3%0A%5B2%2C+4%2C+-3%2C+0%2C+-4%5D&version=20.4.1)
**Explanation**
```
       % Implicitly grab the first input (n)
       %     STACK: { 3 }
:      % Create the array [1...n]
       %     STACK: { [1, 2, 3] }
g      % Convert it to a logical array, yielding an array of 1's of length n
       %     STACK: { [1, 1, 1] }
y      % Implicitly grab the second input and duplicate it
       %     STACK: { [2, 4, -3, 0, -4], [1, 1, 1], [2, 4, -3, 0, -4]}
n      % Determine the length of the array
       %     STACK: { [2, 4, -3, 0, -4], [1, 1, 1], 5}
&Z+    % Perform circular convolution
       %     STACK: { [-2, 2, 3, 1, -7] }
       % Implicitly display the result
```
[Answer]
# Mathematica, 29 bytes
```
RotateLeft[#,1-n]~Sum~{n,#2}&
```
Or the same length:
```
ListConvolve[1~Table~#2,#,1]&
```
[Answer]
## CJam (16 bytes)
```
{_2$*ew1fb\,~)>}
```
[Online test suite](http://cjam.aditsu.net/#code=q'%2C-N%25%7B%22-%3E%22%25S*~%3AE%3B%0A%0A%7B_2%24*ew1fb%5C%2C~)%3E%7D%0A%0A~E%3Do%7D%2F&input=%5B2%2C%204%2C%20-3%2C%200%2C%20-4%5D%2C%203%20%20%20%20%20%20%20%20%20%20-%3E%20%20%5B-2%2C%202%2C%203%2C%201%2C%20-7%5D%0A%5B1%2C%202%5D%2C%205%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%3E%20%20%5B7%2C%208%5D%0A%5B2%5D%2C%207%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%3E%20%20%5B14%5D%0A%5B-5%2C%204%2C%200%2C%201%2C%200%2C%20-10%2C%20-4%5D%2C%204%20%20-%3E%20%20%5B-19%2C%20-15%2C%20-5%2C%200%2C%205%2C%20-9%2C%20-13%5D%0A%5B-5%2C%204%2C%200%2C%201%2C%200%2C%20-10%2C%20-4%5D%2C%201%20%20-%3E%20%20%5B-5%2C%204%2C%200%2C%201%2C%200%2C%20-10%2C%20-4%5D%0A%5B-2%2C%20-1%2C%200%2C%201%2C%202%2C%203%5D%2C%205%20%20%20%20%20%20%20-%3E%20%20%5B4%2C%203%2C%202%2C%201%2C%200%2C%205%5D%0A%5B-10%2C%200%2C%2010%5D%2C%204%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%3E%20%20%5B-10%2C%200%2C%2010%5D). This is an anonymous block (function) which takes the array and the length on the stack and leaves an array on the stack.
### Dissection
```
{       e# Declare a block
  _2$*  e#   Repeat the array n times: this guarantees having enough windows even
        e#   if x is only a single element
  ew    e#   Take each window of n elements
  1fb   e#   Sum each of the windows
  \,~)  e#   Compute -n
  >     e#   Take the last n elements of the array of sums
}
```
[Answer]
## Haskell, 57 bytes
```
a#n|l<-length a=[sum[a!!mod j l|j<-[i-n..i-1]]|i<-[1..l]]
```
[Try it online!](https://tio.run/##VYxBDoIwFET3nmIILvsbCyVu4CRNF00gUvwtRnDH3WvRxOBqMm8yb3TLfWBOyZVx45Z4iLd1hOvM8grGFUWYe0zgbWrJeIpSelLWbj5XJSVbm4LzER36@QQ8nj6uOMNUAlqAaoFLDm1Roj7uSqDaYfN32sn1SKj5eLJDfU3qp9PpDQ "Haskell – Try It Online")
Just some index looping and accessing the input list at indices modulo the length of the list.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
ṙC€}S
```
[Try it online!](https://tio.run/##y0rNyan8///hzpnOj5rW1Ab/P7zcSN/9//9oLoVoIx0FEx0FXWMdBQMgZRKro2CsAxQ21FEwArJNQWwQwxzE0DUFKwYqNIQoN4TpMcEvbQiWNgIJweSBHGOYBWCFIGEDiFGxAA "Jelly – Try It Online")
### How it works
```
ṙC€}S  Main link. Arguments: A (array), n (positive integer)
   }   Apply the link to the left to the right argument (n).
 C€      Complement each; map (z -> 1-z) over [1, ..., n], yielding [0, ..., 1-n].
ṙ      Rotate A 0, ..., 1-n units to the left (i.e., 0, ..., n-1 units to the
       right), yielding a 2D array.
    S  Take the sum of the rows.
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~69~~ ~~65~~ 64 bytes
```
r=reverse
s#n=r$init[sum$take n$x++cycle(r s)|x<-scanr(:)[]$r s]
```
[Try it online!](https://tio.run/##jZDRisIwEEXf/YoL5kGxgaZN6a5sv6T0odSAxRolqYsL@@/dmbCViAoOgYSbc28ms2/9wQzDNLnKmW/jvFn4pa2c6G0/1v5yFGN7MLDiutl0P91gVg5@/Xv9kr5rrVtt13UjSGqmY9tbVNidFji73o4QqLMEOoHME6S06QZL5LiVlEAtiaFFiCKkbCI3CRlbCjyr4C4TfMSWwJd4UcGidGyQRWgxDc9zk@rWqZ47VJ@sE8gwXfMpSPmbSWpOeoHEKRnLM8ODiScQUnSYVjaHFHd2TmRv@v@Dx/9HyPQH "Haskell – Try It Online") Example usage: `[2, 4, -3, 0, -4] # 3`.
---
Using `n` *succeeding* instead of *preceding* entries could be ~~50~~ 46 bytes (getting rid of the reverse at the beginning and the end):
```
s#n=init[sum$take n$x++cycle s|x<-scanr(:)[]s]
```
[Try it online!](https://tio.run/##DcVBCoAgEAXQq3zIRZFBZKuok4gLESHRhmgMDLq79TZvtxx9SrVyQ1ugkDXfh8g2epAofe8elzz4LevAztLVLp02bOphA2HDeQXKENCTxCwxKInxbzZooOoH "Haskell – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 12 bytes
```
ËVo rÈ+UgE-Z
```
[Try it online!](https://tio.run/##y0osKPn//3B3WL5C0eEO7dB0V92o//@jDXWMdIx1TGJ1FIwA "Japt – Try It Online")
TIO doesn't support the `Ë`, so the TIO link won't work. Instead, [try it here](https://ethproductions.github.io/japt/?v=1.4.5&code=y1ZvIHLIK1VnRS1a&input=WzEsIDJdLCA1IC1R).
[Answer]
# [Pyth](https://pyth.readthedocs.io), ~~18~~ 16 bytes
*Saved 2 bytes thanks to [@FryAmTheEggman](https://codegolf.stackexchange.com/users/31625/fryamtheeggman)!*
```
JEms<.>*JQ-JhdJl
```
**[Try it here](https://pyth.herokuapp.com/?code=JEms%3C.%3E%2aJQ-JhdJl&input=%5B2%2C+4%2C+-3%2C+0%2C+-4%5D%0A3%0A&test_suite_input=%5B2%2C+4%2C+-3%2C+0%2C+-4%5D%0A3%0A&debug=0&input_size=3) or [Verify all the test cases.](https://pyth.herokuapp.com/?code=JEms%3C.%3E%2aJQ-JhdJl&test_suite=1&test_suite_input=%5B2%2C+4%2C+-3%2C+0%2C+-4%5D%0A3%0A%0A%5B1%2C+2%5D%0A5%0A%0A%5B2%5D%0A7%0A%0A%5B-5%2C+4%2C+0%2C+1%2C+0%2C+-10%2C+-4%5D%0A4%0A%0A%5B-5%2C+4%2C+0%2C+1%2C+0%2C+-10%2C+-4%5D%0A1%0A%0A%5B-2%2C+-1%2C+0%2C+1%2C+2%2C+3%5D%0A5%0A%0A%5B-10%2C+0%2C+10%5D%0A4%0A&debug=0&input_size=3)**
Fixed all the flaws at a cost of *-6 bytes*! Thanks a lot to Luis for making me understand the task in chat.
---
# Explanation (to be updated)
```
KEms<>_*QhK-lQhdKU - Full program.
KE                 - Assign the second input to a variable K.
  m              U - Map over the range [0...len(first input)).
       *QhK        - First input * (Second input + 1).
      _            - Reverse.
     >     -lQhd   - All the elements of the above after len(x)-current element-1
    <          K   - Up until the second input.
   s               - Sum.
```
[Answer]
# Java 8, 102 bytes
Lambda (curried) from `int[]` to lambda from `Integer` to `int[]`. Assign to `Function<int[], Function<Integer, int[]>>`.
```
a->n->{int l=a.length,o[]=new int[l],i=0,j;for(;i<l;i++)for(j=i-n;j++<i;)o[i]+=a[(j%l+l)%l];return o;}
```
[Try It Online](https://tio.run/##fVLBitswED07XzGXBQnLYru7vVRxYCkUeugpR@OD6lWy41VGRpJTwuJvT2U5aVpoC7bkefNmxu9JvT7qyg2G@pe38zB@t9hBZ3UI8E0jwfuqGDwedTQQoo4puUPSFvpUJseIVu5G6iI6kl8uH2uk2LTiv5SvFM3eeAGZu9lAh74brfbb8RCgPutqQ9XmPWXB1lpaQ/v4KlzT1mR@5CLbCqzvRa92zjOFa6uwLPkc9DVWpPqyXKPirsG2rHXD@jtbWn5nW@VNHD2BU9O5UKukbxF9kXd0@AKHJJ1to0faNy1ovw98dqJYjPmsg8nhtXSxJEsBiyGqv@WA5mG/8LkJu5WIhbKMKYr4ikHOONS3jgtKCaIcT6v5TcvcK/Xp0hbmbLLoAuVu15hdvUv4g4AnAdWjgPu0PU0CHrn4F/mDgIfE@JgZU5aRjAaWjdB5/bRMv/z/9hSiOUg3RpluD0VL7HYbnr3XpyCjWwxmvx@91MNgTyz3zPr5HwhxzlVWnZ7p/BM)
## Ungolfed lambda
```
a ->
    n -> {
        int
            l = a.length,
            o[] = new int[l],
            i = 0,
            j
        ;
        for (; i < l; i++)
            for (j = i - n; j++ < i; )
                o[i] += a[(j % l + l) % l];
        return o;
    }
```
`(j % l + l) % l` computes a nonnegative remainder for any `j`. Taken from [here](https://stackoverflow.com/a/4412200).
[Answer]
# C, 91 bytes
```
i,j,k,s;f(a,l,n)int*a;{for(i=0;i<l;printf("%d ",s))for(j=n*l+i++,k=n,s=0;k--;)s+=a[j--%l];}
```
[Try it online!](https://tio.run/##fY/daoQwEIXvfYpBWEjWCajx5yL1ScJeiK0lau2ysVfis9tJbKFhsSEQkjnnOyedeO@6fTc44IhW9azFCWdu5uXaqrX/fDDTpMq8TOr@oMeexZdXiNFy7mZDM1@nxCQJjs2MlpSjEIrbpGn1IMRluqltJxt8tGZmPFojoOUelje76PIGDaw5QoEgJEJKR7EpL@qZkyCUCJKr@9diWRxzFQWE/AeRIeSBj5i0y1Oj1NmRHbioApHqU1eha@8Spa@cerkrnT03p3lNqifW32l2mlTp6kjKHf03ii4yCKkQqv@@WWt5YFxBx0gDO1WQvmO07d8)
[Answer]
# Octave, 53 bytes
```
@(x,n)shift(imfilter(x,+!!(1:n),'circular'),fix(n/2))
```
[Try it online!](https://tio.run/##DchBCoAgEADAr@jJXdqozFOn/hEdRJQWzMAs/L11GpjLFfv61laolPA@OBTgM3AsPv/VSQnTkpCU4@yeaLNCClwhDRqx2XTDpkkYEv1MYvwxO4kZ2wc "Octave – Try It Online")
* The `imfilter` function with option `circular` computes the circular convolution at the center of window so the result should be shifted.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 10 bytes
```
.×ŒùOR¹g£R
```
[Try it online!](https://tio.run/##MzBNTDJM/f9f7/D0o5MO7/QPOrQz/dDioP//o3VNdRRMdBQMdBQMwaSuIYgwieUyAQA "05AB1E – Try It Online")
**Explanation**
```
.×           # repeat input_1 input_2 times
  Œù         # push all sublists of size input_2
    O        # sum each
     R       # reverse the list
      ¹g£    # take the first len(input_1) items
         R   # reverse the list
```
[Answer]
# [Perl 6](https://perl6.org), ~~42~~ 39 bytes
```
{@^a;[«+»] map {@a.rotate(-$_)},^$^b}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJKmYPu/2iEu0Tr60GrtQ7tjFXITCxSqHRL1ivJLEktSNXRV4jVrdeJU4pJq/1tzcRUngvRoRBvpKJjoKOga6ygYACmTWB0FY01ruKyhjoIRUMgUSQjEN0fi65qCTQDqNoSYYQgzyIQoVYbIqoxAMjBlQI4xmuVgbSBZA4j5/wE "Perl 6 – Try It Online")
My first Perl 6 entry. Can probably be improved.
[Answer]
# [Kotlin](https://kotlinlang.org), ~~141~~ ~~140~~ 138 bytes
Just a first go
## Submission
```
fun c(a:List<Int>,n:Int):List<Int>{
return (0..(a.size-1)).map{var t=0
for (o in 0..(n-1)){var i=it-o
while(i<0) {i+=a.size};t+=a[i]}
t}}
```
## Beautified
```
fun c(a: List<Int>, n: Int): List<Int> {
    return (0..(a.size - 1)).map {    // Iterate over the items
        var t = 0                     // Start the total at 0
        for (o in 0..(n - 1)) {       // Start at the item, go over the window backwards
            var i = it - o            // -------------------------
            while (i < 0) {           //  Make the index in range
                i += a.size           //
            }                         // -------------------------
            t += a[i]                 // Add the item to the total
        }
        t                             // Return the total
    }
}
```
## [TryItOnline](https://tio.run/##nVPBToQwFLz3K56eaAQCyGYVxcSDJiYmJurNeGh2WW3EdtMWN7rh29f3ygKuiXuwh7bMmzcMZfqmXS3VZrNoFMwCUdxK685vlLsIVYELH4E1M5VrjIIgieNAxFZ@VVHKefwulusPYcCVCVtoA4EGqYBIiuq@JkvpIs1Wr7KuAnmecFjLo7ITac8cbp/kc8tc2zI2F07ArBbWwn2jgg9Ro96ycQWM5oDQlVRzvSoAkQ7Qjdvlccbow96FJB0jzAtYZ6R6KR78gjYY4KBmLFoocTHi824ReLwf5KNGUcSzEPIQouMQElxyHgJut7UIi5kHUqxNOQ//UsF6hq2ToXUawskePpGnAznN91CjiXeYeBPkMe2N5qPR9JQKyCR24o1EHjr@l3I6Kv/B2aOaEatvoePbOZjcn2fW6032KdG7SCb59bEDzH1rN/uoUiIwrPTv@yz0eejChImYESv2EQyJGXe54wNbLjDzHfug9Izu6aciDfdq9Aoura2Mk1pdGaNNcPhYWQfXAi/G/HDUbNk4LzGqrlZb6hIvhqe2m803)
# Edits
* Removed newline on before last closing bracket
[Answer]
# [Röda](https://github.com/fergusq/roda), 52 bytes
```
f a,n{(a*n)|slide n|tail#a*n|{head n|sum}while open}
```
[Try it online!](https://tio.run/##hU9LCoMwEF0npxgsFJUIfumqJwmhhCZiQBMxli7Us9uYqnRXGJiZ95nhDUbwda2BEz2FPNbRbFslJOh55Kq9OGSeGsmFA@yrW96NaiWYXupl7bjSMGE0Sjs@ntxKC3egGCFKcwIlgaQgkLpWMgIFI57JCORurfZ1m2/7nFTe5RzZ15cd5vKvIjsU@YYektz/PZ95@cak/iZGDGNUmwHOBOAS/cQRxvn6QekxDGhAgNZhfNIRuzpoKxZEGAmjJV7WDw "Röda – Try It Online")
Explanation:
```
f a,n{
  (a*n)|    /* Push the items in a n times to the stream */
  slide n|  /* Create a sliding block of length n */
  tail#a*n| /* Push the last n*len(a) values in the stream to the stream */
  {         /* While there are elements in the stream (stream is open): */
    head n| /*   Pull n values from the stream */
    sum     /*   Sum them and push the sum to the stream */
  } while open
}
```
[Answer]
# JavaScript ES6 ~~80~~ 78 bytes
```
x=>n=>x.map((_,i)=>eval('for(L=x.length,N=0,j=++i-n;j<i;j++)N+=x[(j%L+L)%L]'))
```
2 bytes saved thanks to Neil
### Usage:
```
f=x=>n=>x.map((_,i)=>eval('for(L=x.length,N=0,j=++i-n;j<i;j++)N+=x[(j%L+L)%L]'))
f([2, 4, -3, 0, -4])(3)
```
[Answer]
# [Perl 5](https://www.perl.org/), 66 + 1 (-a) = 67 bytes
```
$n=pop@F;$,=$";say map{$q=$c++;$t=0;$t+=$F[$q--%@F]for 1..$n;$t}@F
```
[Try it online!](https://tio.run/##K0gtyjH9/18lz7Ygv8DBzVpFx1ZFybo4sVIhN7GgWqXQViVZW9tapcTWAEho26q4RasU6uqqOrjFpuUXKRjq6ankASVqHdz@/zdUMFIw/ZdfUJKZn1f8X9fXVM/A0OC/biIA "Perl 5 – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), ~~69~~ 61 bytes
**- 8 bytes Thanks a lot @muru** 
```
lambda x,n:[sum((x[-n+1:]+x*n)[i:i+n])for i in range(len(x))]
```
[Try it online!](https://tio.run/##fVDdCoIwFL7OpziXWx7B@UMg@CRrFwu1FnoUM7Cnt21iGES7Gd/v2c7wmm49JUtTnpdWd5dKw4xUyMezY2yWEYWiUOF8JC5NYUJSvOlHMGAIRk3XmrU1sZlztVR60qUMDkwmCBlClCLE9soUQgocnSIwsShfwQ8bR/gcb7HkaU9@qVHuG2xarB1iK8ps5r9DuFZvSRy9eSxI1xfu57iU02NfHajA7wDvbgvu20UAw2hogoZZli9v "Python 2 – Try It Online")
Explanation:
First we need to ensure there is enough numbers on the left of the original list, this is acheived by the `x*n+x` part.
For, example: `[2,4,-3,0,4],5`:
```
                   ,2,4,-3,0,-4
 ....-4,2,4,-3,0,-4,2,4,-3,0,-4
```
Then we shall reverse the list:
```
 <original->
 -4,0,-3,4,2, -4,0,-3, 4........
           <-2's block->     
```
Next we obtain corresponding blocks for each element by `[len(x)+~i:][:n]`. The slice will be reverse i.e. 2 will gain a block: `[2,-4,0,-3,4]` which is reverse of the expected `[4,-3,0,-4,2]`, but we need the sum after all. So, this works. :)
[Answer]
# [R](https://www.r-project.org/), ~~101~~ ~~93~~ 89 bytes
```
function(x,n,w=sum(x|1)){for(j in 1:w)F=c(F,sum(c(tail(rep(x,n),n-1),x)[1:n+j-1]))
F[-1]}
```
[Try it online!](https://tio.run/##JYvNCsIwDMfve4oeG0zBrPMy6LUvMTxIsdChmdSNFdRnr6le8v/6Jdfoatw4rGlhXZBxd8/trsubAF5xyXpWiRWNO3gXtMc2Br1e0k3n66N9ALIhwAITjXyYDZ0BOj@JfmoUtkc1oDIW1VFkALTQtZ6wBzz9vRHI0I@QK8G2qX4B "R – Try It Online")
[Answer]
# [K (oK)](https://github.com/JohnEarnest/ok), 18 bytes
**Solution:**
```
{+/+y':(1-y+#x)#x}
```
[Try it online!](https://tio.run/##y9bNz/7/v1pbX7tS3UrDULdSW7lCU7miNtpIwURB11jBQEHXxNo49v9/AA "K (oK) – Try It Online")
**Examples:**
```
{+/+y':(1-y+#x)#x}[1 2;5]
7 8
{+/+y':(1-y+#x)#x}[-5 4 0 1 0 -10 -4;4]
-19 -15 -5 0 5 -9 -13
{+/+y':(1-y+#x)#x}[-10 0 10;4]
-10 0 10
```
**Explanation:**
Was about to post a **31 byte** solution then I remembered that oK has a [built-in](https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md#verb-reference) for sliding windows...
```
{+/+y':(1-y+#x)#x} / the solution
{                } / lambda with implicit x and y parameters
               #x  / take (#) from list x
       (    #x)    / length of x
          y+       / add y (window size)
        1-         / subtract from 1 to give a negative
    y':            / sliding window of size y
   +               / flip
 +/                / sum
```
**Bonus:**
The **31 byte** solution that also works in [K4](http://kx.com/download/):
```
q)k){+/+x#y#'|+(:':\|(1-y+x:#x)#x)}[2 4 -3 0 -4;3]
-2 2 3 1 -7
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
£gVonY)x
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=o2dWb25ZKXg&input=WzIgNCAtMyAwIC00XQoz)
```
£gVonY)x     :Implicit input of array U=x & integer V=n
£            :Map each 0-based index Y in U
 g           :  Get the elements in U at 0-based indices
  Vo         :    Range [0,V)
    nY       :    Subtract each from Y
      )      :  End indexing
       x     :  Reduce by addition
```
]