546A - Soldier And Bananas

Reduce summation to closed form and solve the equation.

Time complexity: $O(1)$

Memory complexity: $O(1)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int k, n, w;
    cin >> k >> n >> w;
    int x = max(k * (w * (w + 1)) / 2 - n, 0);
    cout << x << endl;
    return 0;
}