g | x | w | all
Bytes Lang Time Link
024TIBASIC TI84 Plus CE Python250819T133820Zmadeforl
033AWK241203T193818Zxrs
023Uiua240620T101221ZJoonas
007vemf240620T123151Ztaswelll
nanPxem240620T113345Z鳴神裁四点一号
031Desmos231122T025148ZYouserna
017TIBASIC231122T024357ZYouserna
007Uiua231121T080939Zchunes
255Nibbles230622T111634ZDominic
024Befunge98 PyFunge230622T064520ZJo King
018jq230622T003710Zdon_aman
040C clang230621T205454Zc--
065sed 4.2.2230621T202237ZYelp
046Swift230621T185416ZBbrk24
004Thunno 2 s230621T163238ZThe Thon
033JavaScript Node.js230621T181025Znoodle p
056C gcc230118T182623ZPeter
028Arturo230118T101133Zchunes
018Ly220219T081011Zcnamejj
009Risky210724T215237ZAdam
058tinylisp220204T161641ZGiuseppe
010Pip200829T130036ZRazetime
3037TIBASIC210725T201939Zorangey
019Julia 1.0201119T135755ZMarcMush
005Vyxal s210602T041809Zwasif
023Factor + math.unicode210602T034328Zchunes
004Stax201120T052048ZRazetime
004Jelly201120T045150ZUnrelate
029PowerShell Core201120T033021ZJulian
013K oK200713T160330Zmkst
006Japt h201119T114822ZShaggy
009Charcoal200711T110022ZNeil
026MAWP200825T103258ZRazetime
027Befunge98 PyFunge200711T073720Zovs
407Pyramid Scheme200804T100504ZJo King
029Perl 6200804T040344Zsugarfi
019Ruby200802T104909ZRazetime
053C gcc200731T161022Zkaadmy
045Rust200731T201211ZTehPers
016Wolfram Language Mathematica200711T214700Zatt
009MATL200731T151116ZMukundan
1619Pyramid Scheme200725T231531ZMarcinKo
022Scala200723T155623Zuser
058Lua200722T185323Zval - di
025excel200721T182227ZConor Ja
051Emacs Lisp with dash library200712T122342ZDaanturo
021Raku200716T011132ZJo King
013Golfscript200715T173738ZpsIQos
021dc200715T161458ZSandra
3320Q'Nial200714T220156ZM L
019Perl 5 + pi200714T122053Zmsh210
035R200713T153101ZRobin Ry
041Clojure200713T223409ZM Smith
018x8616 machine code200712T204307Z640KB
017Haskell200711T094313Zxnor
005Jelly200713T113445ZJonathan
081SimpleTemplate200713T090325ZIsmael M
040TSQL200713T084528Zt-clause
005APL Dyalog Unicode200713T075936ZBubbler
009APL Dyalog Extended200713T073148ZAdá
031Io200711T081656Zuser9206
00505AB1E200711T082449Zuser9206
020Raku200713T020304Zuser0721
041PHP200712T171109Z640KB
036Vimscript 36 Bytes200711T221017ZJhal
025Zsh200711T214846ZGammaFun
007Pyth200711T164757ZMukundan
023JavaScript ES6200711T092726ZArnauld
031Retina 0.8.2200711T112529ZNeil
027Python 3200711T103005ZNoodle9
027Python 3200711T102734ZMukundan
003J200711T095633Zxash
019Wolfram Language Mathematica200711T085118ZZaMoC
007Japt200711T082758ZMukundan
019Perl 5 + pa MListUtil+sum200711T072150ZDom Hast

TI-BASIC (TI-84 Plus CE Python), 24 bytes

Input L₁
Input N
L₁(dim(L₁
Ans+sum(L₁N)-AnsN

AWK, 39 33 bytes

{for(;i++<NF-2;)x+=$i}$0=x*$NF+$i

Attempt This Online!

{for(i=NF-1;--i;)s+=$i}$0=s*$NF+$(NF-1)
{for(i=NF-1;--i;)   # skip last two numbers
s+=$i}              # sum
$0=                 # set output
s*$NF               # multiply by last number
+$(NF-1)            # add second to last

Uiua, 23 bytes, 12 characters

/+⊂⊙×↙1:↘1.⇌

Try it on Uiua Pad!

vemf, 7 bytes

├¡╤1«╓ë

Function that takes A as a left argument, and N as a right argument. the mixed-radix Decode ë needs for some reason a full radix list like 31415 ë 1111Φ. ¡╤1« is just a cheeky trick to get the list at the right

31415┘(¡╤1«)Φ
       ¡    → ΦΦΦΦΦ "Translate" index each α in β. but β is a scalar so it's α╕►β
        ╤1  → Φ1111 Scan constant ends up modifying all but the first one
          « → 1111Φ Shift that one to the right. ♠ would work too

A solution using reduce (α»╧├,èβ or α»╧│*β╙+) ends up being about the same because of dealing with binding N, and right folds are annoying. Try it online

Pxem, 0 + 95 = 95 bytes

Filename

.w.t.m.+._.c.a.s.m.-._.!.m.+.n

Try it online!

Usage

Each item in A, end with 0, then N

How it works

Store A[i] or 0.

        ._

Break loop if 0 is given.

.w        .c.a

Remember previous A[i]. Sum each item in A so far.

  .t.m.+

Subtract last item from that sum.

              .s.m.-

Get N to multiply.

                    ._.!

Add last item of A to output the result.

                        .m.+.n

Desmos, 32 31 bytes

f(a,n)=na.total-(n-1)a[a.count]

Try it on Desmos

-1 byte thanks to Aiden Chow.

TI-BASIC, 17 bytes

Input N
Nsum(Ans)-(N-1)Ans(dim(Ans

Takes input of \$A\$ in Ans.

Alternatives that are also 17 bytes:

Input N
sum(Ansmax(1,N(N>cumSum(1 or Ans
Input N
sum(AnsNaugment(1 or ΔList(Ans),{N⁻¹

Uiua, 7 bytes

/+⍜↘ׯ1

Try it!

/+⍜↘ׯ1
     ¯1  # push negative one
  ⍜↘×    # multiply all but last element by input
/+       # sum

Nibbles, 2.5 bytes (5 nibbles)

/@+*_
/@+*_       # full program
/@+*_$@     # with implicit args added;
/           # fold from right over
 @          # arg2
            # with function:
  +         #   add
   *        #   the product of
    _       #     arg1
     $      #     and the left-hand element
      @     #   to the right-hand element

enter image description here

Befunge-98 (PyFunge), 24 bytes

j&0#v&:61p+2
g+.@>'.-*61

Try it online!

Ended up converging on a similar layout to ovs' answer, though with a different strategy:

Explanation:

 &0          Read numeric input (n) and push a zero on top as sum
   #         Skip into the main loop
     &:61p+  Read a number in, save a copy in (6,1) and add it to the sum
j          2 Jump over the initialisation and continue the loop
    v&       When we're out of input, go to the next line

At this point, our stack looks like `n sum`

    >'.-     Retrieve our saved last value and subtract it from the sum
        *    Multiply by n
g+       61  Retrieve our saved value and add it back to the sum
  .@         Print and exit

jq, 18 bytes

(add-last)*$n+last

Try it online!

C (clang), 40 bytes

f(*a,l,n){return--l?*a*n+f(a+1,l,n):*a;}

Try it online!

sed 4.2.2, 65 bytes

s/<(!*)><(!*)>$/%\2%\1/
s/><//g
:a
s/!>%(!*)%/>%\1%\1/
ta
s/.*%//

Try it online!

Input is a list exclamation marks surrounded by <>. n is given as the last element of the list. Output is just exclamation marks.

Example where A = [3,1,4,1,5] and n = 1:

<!!!><!><!!!!><!><!!!!!><!> => !!!!!!!!!!!!!!

Swift, 46 bytes

{a,n in a.dropLast().reduce(a.last!){$0+n*$1}}

Try it online!

Type signature: <C: BidirectionalCollection where C.Element: Numeric>(C, C.Element) -> C.Element. (That syntax isn't quite legal Swift for lack of generic lambdas, but you know what I mean.)

For each element ($1) in a except the last one, add n*$1 to the running total. The total starts at a.last instead of zero.

Thunno 2 s, 4 bytes

×Ẹ¹÷

Attempt This Online!

Thunno 2, 5 bytes

µʋ¹×+

Attempt This Online!

Suggested by @noodleman.

Explanation

×Ẹ¹÷  # Implicit input
×     # Multiply each item in the list by the number
 Ẹ    # Dump onto stack, last item on top
  ¹÷  # Divide this number by the second input
      # Implicit output of summed stack
µʋ¹×+ # Implicit input
µʋ    # Right-reduce the input list by:
  ¹×  #  Multiply by the second input
    + #  And add this to the cumulative total
      # Implicit output

JavaScript (Node.js), 33 bytes

n=>a=>a.reduceRight((x,c)=>x+c*n)

Attempt This Online!

Not as short as the eval solution but uses a very similar method.

C (gcc), 56 bytes

s,i;f(v,l,f)int*v;{for(i=0;i<l-1;s+=v[i++]);return s*f;}

Try It Online!

f takes a vector (v), the length of the vector (l) and the factor to multiply the vector with (f) as arguments. It sums up the values in the vector, except for the last one (in s) and returns the sum multiplied by the factor.

Arturo, 28 bytes

$[a,n][+last a n*sum chop a]

Try it

$[a,n][           ; a function taking args a and n
  +last a         ; add the last elt in a to
  n*sum chop a    ; n times the sum of a without its last elt
]                 ; end function

Ly, 18 bytes

&n&spp&+>l<s>l*+u;

Try it online!

&n                 - read all the numbers into the stack
  &s               - save the entire stack to the backup cell
    pp             - delete top two entries on the stack
      &+           - sum the stack
        >l         - switch to clean stack, load original input
          <s       - switch back, save summary to backup cell
            >l     - switch stacks, load save sum of 2nd to N entries
              *    - multiple top two entries on stack
               +   - add top two stack entries
                u  - print top of stack as a number
                 ; - exit the code to avoid printing the rest of the stack

Risky, 9 bytes

-_{1+0+00_?+0+?*?

Try it online!

tinylisp, 58 bytes

(load library
(d S(q((A N)(i(t A)(a(*(h A)N)(S(t A)N))(h A

Try it online!

tinylisp, 78 65 bytes

(d S(q((A N)(i(t A)(i(h A)(a(S(c(s(h A)1)(t A))N)N)(S(t A)N))(h A

Try it online!

Solution without using library, which is only for *.

-13 bytes thanks to DLosc

Pip, 10 bytes

a@v+b*$+Ha

Attempt This Online!

-2 from DLosc.

TI-BASIC, 30 bytes (on-calc) / 37 bytes (as text)

Assumes list variable L₁ contains the array and variable N contains n.

dim(L₁
Nsum(L₁,1,Ans-1)+L₁(Ans

Explanation

Closing parentheses can be left out at the end of lines in TI-BASIC.

Julia 1.0, 21 19 bytes

A$n=pop!(A)+sum(A)n

Try it online!

Vyxal s, 5 bytes

*ṫ⁰/J

Try it Online!

Factor + math.unicode, 23 bytes

[ dup pop -rot Σ * + ]

Try it online!

Stax, 6 4 bytes

╡÷E@

Run and debug it

Jelly, 4 bytes

Ṫṭ×S

Try it online!

Ṫ       Pop the last element of the left argument,
 ṭ      append it to
  ×     the right argument times what's left of the left argument,
   S    and sum.

A more fun solution, which borrows Jonathan Allan's base conversion trick:

Jelly, 5 bytes

S,¥/ḅ

Try it online!

   /     Reduce the left argument by
 ,       pair right with
S ¥      the sum of left,
    ḅ    and convert from base right.

Bonus: Ä-.ịḅ’} is a whole 7 bytes, and doesn't even work if the left argument only has one element, but it's just kind of funny.

PowerShell Core, 29 bytes

param($a,$b)$a-join"*$b+"|iex

Try it online!

^ inspired by Arnauld's solution

My original one

PowerShell Core, 75 bytes

param($a,$b)(($a[($l=$a.Length-1)]+$b*($a[0..($l-1)]-join'+'|iex)),$a)[!$l]

Try it online!

K (oK), 14 13 bytes

Solution:

{*|x+/y*-1_x}

Try it online!

Explanation:

Couldn't figure out a smart way of solving this.

{*|x+/y*-1_x} / the solution
{           } / lambda taking implicity x, y
        -1_x  / drop (_) 1 element from end of x
      y*      / multiply by y
   x+/        / sum up with x as accumulator
 *|           / take last (reverse, first)

Notes:

Japt -h, 6 bytes

Ç+Ux*V

Try it

Charcoal, 10 9 bytes

I⁺⊟θ×η↨θ¹

Try it online! Link is to verbose version of code. Presumably Charcoal has the same problem as IO when it comes to summing empty lists. Edit: saved 1 byte by converting from base 1 instead. Explanation:

   θ        `A`
  ⊟         Remove last element
 ⁺          Added to
       θ    Remaining elements of `A`
      ↨ ¹   Take the sum by converting from base 1
    ×       Multiplied by
     η      `n`
I           Cast to string
            Implicitly print

MAWP, 26 bytes

%@_2A<\:.>2M3A[1A~M~]%\WM:

Now it works properly on the testcases. Works on MAWP 1.1's integer input.

Try it!

Befunge-98 (PyFunge), 29 27 bytes

j&10p#v&\10g*\4
_\.@  >+\:#

Try it online! Input is first N, then A. Note that there has to be a trailing space.

Animation of the code:

enter image description here

The pilcrow (¶) represents a newline (value 10) in the grid.

Pyramid Scheme, 407 bytes

     ^      ^
    /l\    /+\
   /oop\  ^---^
  ^-----^ -  /x\
 / \   / \   ---
/arg\ /set\
-----^-----^
    /2\   /+\
    ---  ^---^
        ^-  /1\
       ^-   ---
      ^-
     /]\
    ^---^
   / \ /2\
  /set\---
 ^-----^
/x\   ^- 
---  /]\
    ^---^ 
   ^-  /#\
  / \  ---^
 /set\   / \
^-----^ /arg\
-    /+\-----^
    ^---^   /2\
   /*\  -   ---
  ^---^
 ^-  /#\
/x\ ^---
---/ \
  /arg\
 ^-----
/1\
---

Try it online!

Takes input through command arguments, with n as the first argument. This basically implements the algorithm:

i = 2
x = 0
o = 0
while args[i]:
  o += x*args[1]
  x = args[i]
  i += 1

print(o + x)

But with more nesting and some shortcuts, like using the variable 2.

Perl 6, 29 bytes

->$a,$b {$a.pop+([+] $a)*$b};

Try it online!

Ruby, 46 19 bytes

->a,n{eval a*"*n+"}

Courtesy of petStorm.

Old answer:

n,*A,l=gets.split(' ').map(&:to_i)
p A.sum*n+l

C (gcc), 53 bytes

Saved 2 bytes thanks to ceilingcat

s;f(a,n)int*a,n;{for(s=0;a[1];s+=*a++);return*a+s*n;}

Try it online!

Rust, 45 bytes

|a,n|a.pop().unwrap()+a.iter().sum::<i64>()*n

Try it online

The expected argument types are a: &mut Vec<i64> and n: i64.

Wolfram Language (Mathematica), 16 bytes

a___~f~b_=+a#+b&

Try it online!

This one doesn't need a strange-looking input.

Takes input as f[A][n].


older, 17 bytes

#//.a_@b_:>a#2+b&

Try it online!

Takes a singly linked list as input: if X is a value and L is a list, X is the single-element list with element X, and X[L] is the list with element X, followed by the elements of L. For example, 3@1@4@1@5=3[1[4[1[5]]]] is the list with 3, 1, 4, 1, 5.

Per this discussion, we can take the first element with Head, and the tail with Last.

MATL, 9 bytes

*1G0)0Z(s

Try it online!

Explanation

*1G0)0Z(s
*          % Multiply the inputs and push it onto the stack
 1G        % Push first input onto the stack
   0)      % Replace top stack element with its last element
     0Z(   % Set last element of multiplied inputs to top element in stack
        s  % Replace top stack element with its sum

Pyramid Scheme, 7747 3668 1985 1732 1619 bytes

Edits

    ^  ^        ^       ^        ^  ^        ^        ^        ^
   / \ -^      / \     / \      / \ -^      / \      / \      / \
  /set\ -^    / do\   /set\    /   \ -^    /set\    /   \    /out\
 ^-----^ -^  ^-----^ ^-----^  / set \ -^  ^-----^  /     \   -----^
/l\   / \ -^/c\   ^-/n\   ^- ^-------^ -^/i\   ^- / loop  \      /+\
---  /arg\^----  ^- ---  ^- /N\     /#\^----  ^- ^---------^    ^---^
    ^------^    / \     /-\ ---    ^----^    ^- / \       / \  /s\ /#\
   / \    ^-   ^---^   ^---^      / \  ^-   ^- /<=>\     ^---^ --- ---^
  /99 \  ^-   / \ / \ /n\ /1\    /arg\^-   ^- ^-----^   / \ / \      / \
  ----- ^-   ^---/   \--- ---   ^------^  ^- /i\   /0\ ^---/set\    /arg\
       ^-   / \  -----^        /n\    ^- /-\ ---   ---/ \ ^-----^   -----^
      ^-   ^---      / \       ---   ^- ^---^        ^---/s\   / \      /-\
     ^-   / \       /set\           ^- /n\ /1\      / \  ---  / + \    ^---^
    ^-   /set\     ^-----^         / \ --- ---     ^---      ^-----^  /n\ /1\
   ^-   ^-----^   /c\   /!\       /set\           / \       /s\   /*\ --- ---
   -^  /n\   /+\  ---  ^---      ^-----^         /set\      ---  ^---^
   / \ ---  ^---^     /=\       /s\   /0\       ^-----^         /#\ /N\
  /set\    /n\ /1\   ^---^      ---   ---      /i\   /-\       ^--- ---
 ^-----^   --- ---  / \ /l\                    ---  ^---^     / \
/n\   /0\          /arg\---                        /i\ /1\   /arg\
---   ---         ^-----                           --- ---  ^-----
                 /n\                                       /i\
                 ---                                       ---

Try it online!

Explanation

(set nil (arg 99)) // Make nil

// Count the number of input arguments - n
(set nargin 0)
(do
    cond
    (
        (set nargin (+ nargin 1))
        (set cond (! (= (arg nargin) nil)))
    )
)
(set nargin (- nargin 1))

(set N (# (arg nargin))) // N - the number all but last of the array elements is getting multiplied by

// Add all but last elements of A
(set sum 0)
(set i (- nargin 1))
(loop
    (<=> i 0)
    (
        (set i (- i 1))
        (set sum (+ sum (* (# (arg i)) N))) // A[i] multiplied by N
    )
)
(set sum (+ sum (# (arg (- nargin 1))))) // Add the last element of A
(out sum) // Print

Scala, 22 bytes

a=>a.last+_*a.init.sum

This returns a List[Int] => Int => Int

Try it online

Lua, 58 bytes

r,t=0,{...}for i=2,#t-1 do
r=r+t[i]end print(r*t[1]+t[#t])

Try it online!

Having almost no array functions, Lua doesn't do too well in this challenge.

excel, 25 bytes

Enter A followed by n in one column, followed by this formula

=SUM(R1:R[-3])*R[-1]+R[-2]

Emacs Lisp with dash library: 38 51 bytes

(lambda(n A)(+(car(last A))(* n(-sum(butlast A)))))

(38 bytes was the function body' size only.)

Raku, 21 bytes

{*.join("*$_+").EVAL}

Try it online!

This works the same as the Perl 5 answer and also works with non-mutable lists, unlike the existing Raku answer, which is only one byte smaller than this.

Note: My previous answer of the same size does not work for singleton lists, which I've lodged an issue about.

Golfscript, 13 bytes

~:i;-1%{i*+}*

Try it online!

Explanation: ~ to convert string input to array and integer on stack. :i; assigns \$n\$ to i and pops value. -1% reverses the array and {i*+}* folds the array with (a, b) -> a*n + b

dc, 21 bytes

ss0[+z2<o]dso0rx*ls+p

Examples:

dc -e '10 3 1 4 1 5 ss0[+z2<o]dso0rx*ls+p'
95
dc -e '1 3 1 4 1 5 ss0[+z2<o]dso0rx*ls+p'
14
dc -e '999 1 ss0[+z2<o]dso0rx*ls+p'
1

Q'Nial, 33 bytes (20 bytes without operator definition)

s is op n a{+link[n*front,last]a}   %full operator definition

+link[n*front,last]a                %simple function

Explanation:

+                                   sum, reduce by +
 link                               list of the items of the argument
     [                              atlas (argument of link operation), point-free notation
      n*                            n *
        front                       all elements but the last of the argument
             ,
              last                  last element of the argument
                  ]                 end atlas
                   a                array a (argument of atlas)

Intermediate results, for n=10 and a=3 1 4 1 5

         10 s 3 1 4 1 5
or
         s 10 (3 1 4 1 5)

or
         +link[10*front,last]3 1 4 1 5

         [n*front,last] a
+-------------+-+
|+--+--+--+--+|5|
||30|10|40|10|| |
|+--+--+--+--+| |
+-------------+-+

         link[n*front,last] a
+--+--+--+--+-+
|30|10|40|10|5|
+--+--+--+--+-+

         +link[n*front,last]a
95

Perl 5 + -pi, 19 bytes

s/,/*$^I+/g;$_=eval

For example, for multiplicand 10, perl -pi10 -e's/,/*$^I+/g;$_=eval

Try it online. Idea stolen from this J answer. Accuracy improved by Dom Hastings in comments on this answer (many thanks!).

R, 37 36 35 bytes

-2 bytes with help from Giuseppe

function(l,n)rev(l)%*%n^(seq(!l)>1)

Try it online!

Reverse the vector, and perform dot product with the vector \$(n^0, n^1, n^1, \ldots,n^1) = (1, n, n,\ldots, n)\$.

I just discovered this behaviour of seq, which gains 1 byte on item 4 of this tip: seq(!l) is equivalent to seq(along.with = l) (giving the vector 1 2 3 ... length(l)) in all situations, even if l is of length 1. That is because !l is a logical, not an integer, and so we avoid the call to seq.int when l is a (length 1) integer.

Clojure 41 bytes

#(+(last %1)(* %2(apply +(butlast %1))))

Unfortunately, + does have to be applyed.

Try It Online

x86-16 machine code, 18 bytes

33 DB       XOR  BX, BX         ; clear running sum
49          DEC  CX             ; decrement array length
74 09       JZ   ADD_LAST       ; handle array length of 1 case
        LOOP_SUM:
AD          LODSW               ; load next value into AX
03 D8       ADD  BX, AX         ; BX = BX + AX
E2 FB       LOOP LOOP_SUM       ; keep looping
93          XCHG AX, BX         ; move sum into AX
F7 E2       MUL  DX             ; DX:AX = AX * DX
93          XCHG AX, BX         ; move result back to BX
        ADD_LAST:
AD          LODSW               ; load last value into AX
03 C3       ADD  AX, BX         ; AX = AX + BX
C3          RET                 ; return to caller

As a callable function: [SI] to input array, CX array length, DX = N. Output to AX.

Rather than make an elaborate test program, here's it being run using DOS DEBUG, entering the input array into memory and setting registers as they would be called:

enter image description here

Explanation of above:

Enter input array into memory address DS:200 as 16-bit, little-endian words:

-e 200 3 0 1 0 4 0 1 0 5 0

Point SI to this input array:

-r SI
:200

Set CX to array's length:

-r CX
:5

Set N to 10 (0xA in hex):

-r DX
:A

Execute and stop before last instruction (RET will "return to DOS" and clobber registers):

-g 111

Result is AX=005F or 95 in decimal.

Haskell, 17 bytes

foldr1.((+).).(*)

Try it online!

It turns out this this was close to a port of the intended J solution. The pointfree function ((+).).(*) takes the argument n to the map \a b->a*n+b, that is, to add n times the left value to the right value. This creates the same "verb" as J used, and the foldr1 does the same a J's automatic right to left evaluation. It starts with the rightmost value in the list, which never gets multiplied by n, and applies it right-to-left, effectively increasing the sum so far with n times to the new element.

Jelly, 5 bytes

ṪṭSƊḅ

A dyadic Link accepting a list of numbers on the left and a number on the right which yields a number.

Try it online!

ṪṭSƊḅ - Link: list of numbers, A; number n
   Ɗ  - last three links as a monad - f(A):
Ṫ     -   remove the tail (of A) and yield its value
  S   -   sum (the remaining elements in A)
 ṭ    -   tack -> [sum_of_remaining, tail]
    ḅ  - convert from base (n) -> n×sum_of_remaining+1×tail

SimpleTemplate, 81 bytes

It's fairly chunky :/
Mostly due to limitations in my language...

{@eachargv.0}{@ifloop.last is equal0}{@set*_ _,argv.1}{@/}{@incby_ R}{@/}{@echoR}

Takes a string of numbers or an array as the first argument, and the n as the 2nd argument.

You can try this on http://sandbox.onlinephpfunctions.com/code/4c7290781560c876ff1e72e5d1680ed7b98861a3


Ungolfed:

The code above is a little weird to read...

Here's a more readable version:

{@set result 0}
{@each argv.0 as number}
    {@if loop.last is equal to false}
        {@set* number number, argv.1}
    {@/}
    {@inc by number result}
{@/}
{@echo result}

Notes:

T-SQL, 40 bytes

I am using a table instead of an array, sql doesn't have arrays

The test uses a temporary table instead of a real table, because of lack of permissions to create a table.

SELECT sum(a*@-i/@@rowcount*a*~-@)FROM t

Try it online

APL (Dyalog Unicode), 5 bytes

+⍣⎕/⎕

Try it online!

A full program, which pretty much works like the 3-byte J solution. Takes two lines of input, \$A\$ first and \$n\$ second.

How it works

+⍣⎕/⎕
    ⎕  ⍝ Take the input A
   /   ⍝ Reduce by...
+      ⍝   Add the left argument
 ⍣⎕    ⍝   n times

For n=10 and A = 3 1 4 1 5, this becomes:
+⍣10/3 1 4 1 5
3 (+⍣10) 1 (+⍣10) 4 (+⍣10) 1 (+⍣10) 5
3 added 10 times to
         1 added 10 times to
                  4 added 10 times to
                           1 added 10 times to
                                    5

APL (Dyalog Extended), 8 bytes

1¨⍛,⊥0,⊣

Try it online!

A longer but more interesting one. A tacit dyadic function that takes \$A\$ on its left and \$n\$ on the right.

Uses mixed base conversion , which does the following:

Base:        1  1  1  ... 1    n
Digit value: n  n  n  ... n    1
Array value: 0  a1 a2 ... ax-1 ax
Total: a1n + a2n + ... + ax-1n + ax

How the code works

1¨⍛,⊥0,⊣  ⍝ Input: left=A, right=n
1¨        ⍝ An array of ones as long as A
  ⍛,      ⍝ Append n, which becomes the base
     0,⊣  ⍝ A prepended with single zero, which becomes the values
    ⊥     ⍝ Mixed base conversion as described above

APL (Dyalog Extended), 9 bytes (SBCS)

Anonymous tacit infix function. Takes \$A\$ as left argument and \$n\$ as right argument.

⊢/+.×+×∘~

Try it online!

×∘~\$A×(1-n)\$

+.×+\$\big(\sum_{i=1}^N A_i×n\big)+\$

⊢/ rightmost element (lit. right-argument reduction)

So this effectively implements: $$ \Bigg(\bigg(\sum_{i=1}^N A_i×n\bigg)+A×(1-n)\Bigg)_N\\ \bigg(\sum_{i=1}^N A_i×n\bigg)+A_N×(1-n)\\ \bigg(\sum_{i=1}^N A_i×n\bigg)+A_N-n×A_N\\ \bigg(\sum_{i=1}^{N-1} A_i×n\bigg)+A_N $$

Io, 31 bytes

Saves a byte over the 32 byte solution. But it's boring.

method(a,N,a sum*N-a pop*(N-1))

Try it online!

Explanation

method(a, N,
    a sum * N         // Sum the input, multiply by N
  - a pop * (N - 1)   // Minus the last item, multiplied by N - 1
)

Io, 32 bytes

Just as comparison

method(a,N,a push(a pop/N)sum*N)

Try it online!

Explanation

method(a, N,
           a pop           // Extract the last item from the input list
                /N         // Divide it by N
    a push(       )        // And then put it back into the original list
                   sum     // Sum the list
                      *N   // Multiply the value by N
)

05AB1E, 5 bytes

-2 bytes thanks to @KevinCruijssen.

*`²÷O

Try it online!

Explanation

*     Multiply list by second operand
 `    Dump
   ÷  Divide the last item by
  ²   the second operand
    O Sum the stack

05AB1E, 7 bytes

„²*ý.VO

Try it online!

Explanation

„       2-char string
 ²*     (Which does when evaluated) Multiply by the second input
   ý    Join the input list by this
    .V  Evaluate
      O Sum the resulting stack

Raku, 20 bytes

{@^a.pop+$^b*@a.sum}

By using twigils, @^a matches the first arg (the array), and $^b the second (the multiplier).

Try it online!

PHP, 41 bytes

fn($a,$n)=>array_pop($a)+array_sum($a)*$n

Try it online!

Just trying to use all the built-ins!

Vimscript 36 Bytes

Disgusted to report that Arnauld's solution also works for vimscript.

let F={a,n->eval(join(a,"*".n."+"))}

Zsh, 25 bytes

n=`<&0`
<<<$[0${@/#/*n+}]

Try it online!

Takes the list as arguments and N on stdin. Inspired by the JS answer. Prefix each element with *n+ and $[evaluate arithmetically]. We have to add a 0 to the start as well. This is one byte shorter than using the join flag <<<$[${(j:*n+:)@}]

Zsh -P, 24 bytes

a=(0 \*`<&0`+$@)
<<<$[a]

Try it online!

Alternate solution using the -P flag, which enables RC_EXPAND_PARAM to do the same thing.

Pyth, 7 bytes

+*sPQEe

Try it online!

Explanation

+*sPQEe
    Q    # First input
   P     # Remove the last element
  s      # Sum elements
 *   E   # Multiply by the second input
+     e  # Add the last element of the first input

JavaScript (ES6),  28  23 bytes

Saved 3 bytes thanks to @Mukundan314

Expects (A)(n).

A=>n=>eval(A.join`*n+`)

Try it online!

How?

We simply join the input array with "*n+", so that [1,2,3] is turned into "1*n+2*n+3" and evaluate the resulting string.

Retina 0.8.2, 31 bytes

\d+
$*
1(?=.*,1*;(1*)|1*$)
$1
1

Try it online! Link includes test cases. Explanation:

\d+
$*

Convert to unary.

1(?=.*,1*;(1*)|1*$)
$1

Multiply all but the last element of A by n and delete A.

1

Take the sum and convert to decimal.

Python 3, 27 bytes

lambda a,n:a.pop()+sum(a)*n

Try it online!

Python 3, 27 bytes

lambda a,n:a.pop()+sum(a)*n

Port of my Japt solution to python

Try it online!

J, 3 bytes

That was fun to find.

&+/

Try it online!

How it works

10 (&+/) 3 1 4 1 5 will bind 10 as an argument of + as 10&+, one verb that gets inserted between the elements of the list by /. So we have: 3 (10&+) 1 (10&+) 4 (10&+) 1 (10&+) 5. Now x n&v y means that y gets applied to n&v for x times. With J's right to left evaluation we get: to 5 add 1 times 10, add 4 times 10, add 1 times 10, add 3 times 10. A challenge made for J's stranger parts. :-) And because + is commutative, +&/ would also be a valid solution.

Wolfram Language (Mathematica), 19 bytes

#2Tr@Most@#+Last@#&

Try it online!

Japt, 7 bytes

o +V*Ux

Try it online!

Explanation

o +V*Ux
o         // Pop and return last element of first input
  +       // plus
   V*     // second input times
     Ux   // Sum of first input

Perl 5 + -pa -MList::Util+sum, 19 bytes

$_=pop(@F)+<>*sum@F

Try it online!