Table of Contents

ExplanationImplementation

Official Editorial

Explanation

We can find the total cost of ww bananas by calculating the sum of numbers from 1W1 - W and multiplying by the price of the first banana, kk.

Now, we can just compare how much money is needed to buy the bananas.

Implementation

Time Complexity: O(1)\mathcal{O}(1)

Python

first, money, num = map(int, input().split())
cost = num * (num + 1) // 2
cost *= first
print(max(0, cost - money))

C++

#include <iostream>
using namespace std;
int main() {
int first, num;
long long money;
cin >> first >> money >> num;
int cost = num * (num + 1) / 2;
cost *= first;
cout << (max(0, cost - money));
}

Java

import java.io.*;
import java.util.*;
public class SoldierBananas {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int first = input.nextInt();
long money = input.nextInt();
int num = input.nextInt();

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!