abc151_b - Achieve The Goal

Let $x$ be the score needed to achieve a total of $M$ points.

$x = M * N - \sum_{1}^{n - 1} a_i$

If $x <= k$ then it’s achievable, otherwise it’s not.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
ll ceil(ll a, ll b) { return (a + b - 1) / b; }
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, k, m;
    cin >> n >> k >> m;
    vi a(n - 1);
    for (auto &ai : a)
        cin >> ai;
    ll sum = accumulate(begin(a), end(a), 0LL);
    ll x = max(m * n - sum, 0LL);
    x = x > k ? -1 : x;
    cout << x << endl;
    return 0;
}