{ }codelper
accumulatingP0282

A reading marathon that grows every day

Jiwoo runs a reading marathon for n days. On the first day Jiwoo reads a pages. From the second day on, Jiwoo reads the number of pages read the day before plus d. However, Jiwoo never reads more than m pages in one day. If the previous day's pages plus d is greater than m, Jiwoo reads only m pages that day. One line gives a, d, m and n separated by single spaces. Print the total number of pages read over the n days.

Input
1 ≤ a ≤ m ≤ 10,000, 0 ≤ d ≤ 10,000, 1 ≤ n ≤ 100,000 (all integers)
Output
One line with the total pages read over n days, as plain digits without commas
Example 1 · stdin
10 5 22 5
stdout
89
Example 2 · stdin
3 0 3 4
stdout
12
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Accumulating means creating two variables before the loop (today's pages pages = a, the sum total = 0), then on every pass adding with total += pages and changing pages to tomorrow's value.
+= is the same in all 8 languages.
Example 1 is 10 + 15 + 20 + 22 + 22 = 89.
The smaller of two values: Python min(pages + d, m), JS/TS Math.min, Java Math.min, C# Math.Min, C++ std::min (<algorithm>), Craykon Mathf.Min.
The C standard library has no min for integers, so write if (pages > m) pages = m;.
Four traps: (1) Swapping the order — growing first, then adding — drops the a pages of the first day.
(2) Creating the sum variable inside the loop resets it to 0 on every pass.
(3) Forgetting the cap m makes the later days too large.
(4) Counting days from 1 but asking day < n drops the last day (use day <= n, or count from 0 with day < n).
The sum here is at most 1,000,000,000 (one billion), so it fits in an int (up to about 2.1 billion) in Java, C#, C and C++.
With larger ranges you would have to accumulate into a long (long long in C and C++).

Pythonmain.py · 7 lines
a, d, m, n = map(int, input().split())pages = a   # 오늘 읽는 쪽수total = 0   # 지금까지 읽은 쪽수for _ in range(n):    total += pages    pages = min(pages + d, m)  # 내일 읽을 쪽수(m을 넘지 않게)print(total)
JavaScriptmain.js · 8 lines
const [a, d, m, n] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let pages = a; // 오늘 읽는 쪽수let total = 0; // 지금까지 읽은 쪽수for (let day = 1; day <= n; day++) {  total += pages;  pages = Math.min(pages + d, m); // 내일 읽을 쪽수(m을 넘지 않게)}console.log(total);
TypeScriptmain.ts · 9 lines
import { readFileSync } from "node:fs";const [a, d, m, n]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let pages: number = a; // 오늘 읽는 쪽수let total = 0; // 지금까지 읽은 쪽수for (let day = 1; day <= n; day++) {  total += pages;  pages = Math.min(pages + d, m); // 내일 읽을 쪽수(m을 넘지 않게)}console.log(total);
JavaMain.java · 15 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int a = sc.nextInt(), d = sc.nextInt(), m = sc.nextInt(), n = sc.nextInt();        int pages = a;   // 오늘 읽는 쪽수        int total = 0;   // 지금까지 읽은 쪽수(가장 커도 10억이라 int에 담긴다)        for (int day = 1; day <= n; day++) {            total += pages;            pages = Math.min(pages + d, m);  // 내일 읽을 쪽수(m을 넘지 않게)        }        System.out.println(total);    }}
C#Main.cs · 18 lines
using System; class Program{    static void Main()    {        string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int a = int.Parse(parts[0]), d = int.Parse(parts[1]), m = int.Parse(parts[2]), n = int.Parse(parts[3]);        int pages = a;   // 오늘 읽는 쪽수        int total = 0;   // 지금까지 읽은 쪽수(가장 커도 10억이라 int에 담긴다)        for (int day = 1; day <= n; day++)        {            total += pages;            pages = Math.Min(pages + d, m);  // 내일 읽을 쪽수(m을 넘지 않게)        }        Console.WriteLine(total);    }}
C++main.cpp · 14 lines
#include <algorithm>#include <iostream> int main() {    int a, d, m, n;    std::cin >> a >> d >> m >> n;    int pages = a;        // 오늘 읽는 쪽수    int total = 0;        // 지금까지 읽은 쪽수(가장 커도 10억이라 int에 담긴다)    for (int day = 1; day <= n; day++) {        total += pages;        pages = std::min(pages + d, m);  // 내일 읽을 쪽수(m을 넘지 않게)    }    std::cout << total << '\n';}
Cmain.c · 15 lines
#include <stdio.h> int main(void) {    int a, d, m, n;    if (scanf("%d %d %d %d", &a, &d, &m, &n) != 4) return 0;    int pages = a;        // 오늘 읽는 쪽수    int total = 0;        // 지금까지 읽은 쪽수(가장 커도 10억이라 int에 담긴다)    for (int day = 1; day <= n; day++) {        total += pages;        pages += d;       // 내일 읽을 쪽수        if (pages > m) pages = m;    }    printf("%d\n", total);    return 0;}
Craykonmain.cray · 13 lines
수들 = Console.ReadNumbers();a = 수들[0];d = 수들[1];m = 수들[2];n = 수들[3];pages = a; // 오늘 읽는 쪽수total = 0; // 지금까지 읽은 쪽수for (day = 1; day <= n; day++){    total += pages;    pages = Mathf.Min(pages + d, m); // 내일 읽을 쪽수(m을 넘지 않게)}Console.WriteLine(total);