CF 546A Soldier and Bananas

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  ≤  10000 ≤ 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
input
3 17 4
output
13

题目大意:有三个数字 k w n , k代表要买k份水果,但是从1~w的k是变为1k,                              2k。。。等然后现在有n块大意就是还差多钱;


题解:把k提出来,得到一个等差数列,化简 cost = k * (w*w - w) / 2 得到总花费,然              后用总花费减去现在的就是还缺的,特判如果为负数那么就是0 (钱够了);


AC代码:

#include <bits/stdc++.h>
using namespace std ;
int main()
{
    int n , w , k ;
    cin>>k>>n>>w;
    int cost ;
    cost = k*(w*w+w)/2;
    int mini ;
    mini = cost - n ;
    if(mini<=0)  printf("0\n");
    else cout<<mini<<endl;
    return 0 ;
}



你可能感兴趣的:(CF 546A Soldier and Bananas)