Knitting

Interview

Time limit1sMemory limit128 MB

Summary
Given a starting stitch count, a row total, and a repeating pattern of row change values, compute the sum of all row stitch counts for each project until the 0 0 0 terminator.
Level

Medium4 of 10

Topics
Math, Prefix sum, Simulation
Solved
No attempts yet

Problem

Marcia loves to knit, and she wants to know how many stitches a project will take to complete.

Every project starts with a first row containing a given number of stitches. Each later row is obtained from the previous row by adding a value taken from a fixed, repeating pattern. A positive value increases the number of stitches, a negative value decreases it, and 00 leaves it unchanged. After the pattern's last value is used, it repeats from the beginning.

For example, a triangular shawl might start with 33 stitches and add 22 stitches on every following row. Then the rows have 33, 55, 77, …\ldots stitches, and a project of 33 rows uses 3+5+7=153 + 5 + 7 = 15 stitches in total.

A scarf might use a 44-row repeating pattern that adds 66, then subtracts 22, subtracts 22, and finally changes nothing. Starting from 5050 stitches, the rows have 5050, 5656, 5454, 5252, 5252, and (as the pattern repeats) 5858 stitches. A 66-row project therefore uses 50+56+54+52+52+58=32250 + 56 + 54 + 52 + 52 + 58 = 322 stitches.

Given the description of one or more projects, compute the total number of stitches each project takes to complete.

Input

The input describes one or more projects. Each project is given on two lines.

The first line contains three integers:

N M K

where NN (1≤N≤1001 \le N \le 100) is the number of stitches in the first row, MM (1≤M≤10001 \le M \le 1000) is the total number of rows, and KK (1≤K≤1001 \le K \le 100) is the number of rows in the repeating pattern.

The second line contains exactly KK integers, each between −100-100 and 100100 inclusive, describing the pattern: a negative value decreases the stitch count, a positive value increases it, and 00 leaves it unchanged. No project's pattern ever makes a row have 00 or fewer stitches.

The end of input is a line containing three zeros (0 0 0), which is not part of any project.

Output

For each project, print the total number of stitches in the completed project on its own line, in the order the projects are given, with no blank lines between the answers.

Examples4

  1. Example 1

    Input
    3 3 1
    2
    50 6 4
    6 -2 -2 0
    0 0 0
    
    Expected output
    15
    322
    
  2. Example 2

    Input
    10 1 1
    5
    0 0 0
    
    Expected output
    10
    
  3. Example 3

    Input
    7 4 2
    0 0
    0 0 0
    
    Expected output
    28
    
  4. Example 4

    Input
    3 3 1
    2
    5 2 3
    1 -1 2
    0 0 0
    
    Expected output
    15
    11