Codeforces 546A Soldier and Bananas Solution & Explanation

Difficulty : 800

Problem Description

A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i·k dollars for the i-th banana).

He has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas?

Input

The first line contains three positive integers k, n, w (1  ≤  k, w  ≤  1000, 0 ≤ n ≤ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants.

Output

Output one integer — the amount of dollars that the soldier must borrow from his friend. If he doesn’t have to borrow money, output 0.

Examples

Input3 17 4
Output13

Solution

How to sum 1 to n, there have a math formula for it

➡ Sum = (1+n)*n/2

And we can use this formula to write the solution

C# Solution

Solution1

string[] input = Console.ReadLine().Split(' '); //split by space
int k = int.Parse(input[0]);    //dollars per unit
int n = int.Parse(input[1]);    //the dollars he got
int w = int.Parse(input[2]);    //amount that he want to buy

int unit = (1+w)*w/2;
int price = unit*k;



Console.WriteLine(n>=price?0:price-n); 

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] input = scanner.nextLine().split(" ");

        int k = Integer.parseInt(input[0]);    //dollars per unit
        int n = Integer.parseInt(input[1]);    //the dollars he got
        int w = Integer.parseInt(input[2]);    //amount that he want to buy
        
        int unit = (1+w)*w/2;
        int price = unit*k;
        
        System.out.println(n>=price?0:price-n);
        
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 546A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *