# How much will this cost?

Given N items (0 < N <= 50) with prices (which are always integers) P0 ... PN-1 and given amounts of each item A0 ... AN-1, determine how much the total cost will be.

### Examples

N: 2
P0: 2
P1: 3
A0: 1
A1: 4
Result: 14

N: 5
P0: 2
P1: 7
P2: 5
P3: 1
P4: 9
A0: 1
A1: 2
A2: 3
A3: 2
A4: 3
Result: 60

Remember, this is , so the code with the smallest number of bytes wins.

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

• In other words, "find the dot product of two vectors"?
– xnor
Oct 2 '16 at 0:22
• Will the costs always be integers? Oct 2 '16 at 0:26
• @Dennis Yes. They will. Oct 2 '16 at 0:45
• In case it matters, positive integers?
– xnor
Oct 2 '16 at 0:46
• @xnor No, it can be negative Oct 2 '16 at 0:47

# Actually, 1 byte

*

Try it online!

Okay, this is the shortest dot product built-in I could find. >_>

# Jelly, 2 bytes

æ.

Try it online!

A built-in computing the dot product between two input vectors: if, for each 1 ≤ i ≤ n, we buy ai items worth pi each, the formula for the total cost is a1p1 + a2p2 + … +anpn, which is precisely the definition of the dot product.

.

# Usage:

{1, 2, 3}.{4, 5, 6}

(* 32 *)

(sum.).zipWith(*)

Multiply the lists entrywise, then sum. Shorter than importing (23 bytes)

import Data.Vector
vdot

# Python, 36 33 bytes

lambda*x:sum(map(int.__mul__,*x))

Thanks to @xnor for golfing off 3 bytes!

Test it on Ideone.

• If you can take input as a list of lists, you can do lambda*M:sum(x*y for x,y in zip(*M)) and lambda*M:sum(map(int.__mul__,*M)).
– xnor
Oct 2 '16 at 0:31
• How about float.__mul__? Should still be one byte shorter.
– Lynn
Oct 2 '16 at 0:32
• @Lynn Thanks, but we didn't need to handle non-integers after all. Oct 2 '16 at 0:48

Prompt L1,L2
sum(L1L2

# Ruby, 34 23 + 9 = 32 bytes

+9 bytes for -rmatrix flag.

->p,a{Vector[*p].dot a}

See it on eval.in: https://eval.in/653676

• indirectly you use N as count of arrays Oct 2 '16 at 0:05
• That's sort of an existential question. If N was never defined, would the arrays still exist? If the arrays didn't exist, would N? Oct 2 '16 at 0:12

# Pyth, 3 bytes

s*V

(s)ums the (V)ectorized (*)product of the two input arrays.

# MATLAB / Octave, 4 bytes

@dot

This defines an anonymous function.

# C#, 25 bytes

p.Zip(a,(x,y)=>x*y).Sum()

Sample program:

using System.Linq;
using Xunit;
public class Tests {
[Fact]
public void Cost() {
int[] p = new [] { 2, 7, 5, 1, 9 };
int[] a = new [] { 1, 2, 3, 2, 3 };
int r = p.Zip(a, (x, y) => x * y).Sum();
Assert.Equal(60, r);
}
}

# R, 8 18 bytes

sum(scan()*scan())

Now takes input from stdin.

• Submissions must either be full programs or functions. Oct 4 '16 at 19:42

# Julia 1.0, 13 bytes

Building up on Donat's solution. Thanks to Chartz Belatedly for pointing out the issue with inputs

x/y=sum(x.*y)

Try it online!

# MATL, 2 bytes

*s

Try it online!

*     % Element-wise product of two implicit inputs
s     % Sum of array, implicitly displayed

# Java, 67 bytes

a->{int p=0;for(int i=-1;++i<a[0];)p+=a[i+1]*a[i+a[0]+1];return p;}

Slightly ungolfed:

public static int pay(int...a){
int p=0;
for(int i=-1;++i<a[0];){
p+=a[i+1]*a[i+a[0]+1];
}
return p;
}

# Perl 6, 13 bytes

# 60

## Explanation:

{          # bare block lambda with implicit parameter ｢$_｣ [+] # reduce using addition operator [Z*] # reduce using zip meta-operator combined with multiplication operator$_   # the argument ( list of lists )
}

# TI-BASIC, 3 tokens

sum(L₁L₂

Input in L₁ and L₂, output in Ans.

Usage:

{1,2,3→L1
{4,5,6→L2
prgmCOST

Output: 32

# 05AB1E, 3 bytes

øPO

Try it online!

øPO  # full program
O  # sum of...
P   # products of...
# (implicit) each element in...
# implicit input...
ø    # with each element from the first sublist paired with the corresponding element from the second sublist
# implicit output

# R, 5 bytes

%*%

Try it online!

That's the dot-product built-in in R...

# JavaScript, 33 bytes

a=>b=>a.reduce((c,d,e)=>c+d*b[e])

Takes input as ([p[0],...,p[n-1])([a[0],...,a[n-1]]).

# C, 58 56 bytes

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

Input is n: number of elements, p:array 1, a:array 2.

Somewhat un-golfed:

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

# APL (Dyalog Classic), 3 bytes

+.×  ⍝ My code is self-documenting

Try it online!

*

Try it Online!

Magic.

# PHP, 46 Bytes

<?foreach($_GET[a]as$n)$s+=$n[0]*$n[1];echo$s;

48 Bytes

$r Try it online! # BQN, 3 bytes +´× Try it! dot product # Zsh, 31 25 bytes for i ($p)((S+=i*a[++j]))

This is the core function. The challenge didn't say anything about setting up variables or output so that's all external.