Explanation
We can find the total cost of bananas by calculating the sum of numbers from and multiplying by the price of the first banana, .
Now, we can just compare how much money is needed to buy the bananas.
Implementation
Time Complexity:
Python
first, money, num = map(int, input().split())cost = num * (num + 1) // 2cost *= firstprint(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!