Problem Overview

You can find the original problem statement on Codeforces 686A - Free Ice Cream.

The problem describes a scenario in which an ice cream machine is being used by a line of children. Each child either takes ice cream or adds more to the machine. The machine has an initial quantity of ice cream, and we need to simulate the operations while also keeping track of how many children left disappointed when there wasn’t enough ice cream.

Each operation is either of the form +d (add d ice cream) or -d (remove d ice cream). If a -d operation occurs but the machine has less than d ice cream, the child leaves disappointed, and no ice cream is removed. We’re to count the final amount of ice cream and the total number of disappointed children.

C++ Solution

#include <bits/stdc++.h>
using namespace std;

long long power(int x) {
    int ans = 1;
    for (int i = 0; i < x; i++) {
        ans *= 10;
    }
    return ans;
}

long long conTonumber(string s) {
    long long x = 0, y;
    for (long long i = 2; i < s.size(); i++) {
        y = s.size() - i - 1;
        x += (s[i] - 48) * power(y);
    }
    return x;
}

int main() {
    long long n, x, i, y, cnt = 0;
    cin >> n >> x;
    string s;
    for (i = 0; i <= n; i++) {
        getline(cin, s);
        if (s[0] == '+')
            x += conTonumber(s);
        else {
            y = conTonumber(s);
            if (y > x)
                cnt++;
            else
                x -= y;
        }
    }
    cout << x << " " << cnt << endl;
    return 0;
}

Explanation of the Approach

The code does a simple simulation of the entire sequence. Let’s break it down:

1. Reading Input

  • First, we read two integers n and x, where n is the number of events, and x is the initial amount of ice cream.

  • Then we read n operations line by line. Each operation is a string either of the form +d or -d.

2. Processing the Operation

  • If the operation starts with '+', we simply add the amount d to x.

  • If it starts with '-', we first check if x >= d. If yes, we subtract d from x; otherwise, we count one more disappointed child (cnt++).

3. Number Extraction

  • The conTonumber() function is used to extract the number from the operation string. For example, if the input is -10, it converts the string "10" to integer 10.

4. Final Output

  • After all operations are processed, we print the remaining amount of ice cream and the number of disappointed children.

Dry Run Example

Let’s take a small input to understand the logic:

Input:
5 7
-3
-5
+4
-2
-8

Initial Ice Cream: 7
Operations:

  1. -3: Enough ice cream → x becomes 4
  2. -5: Not enough → cnt becomes 1
  3. +4: x becomes 8
  4. -2: x becomes 6
  5. -8: Not enough → cnt becomes 2

Output:
6 2

Edge Cases to Consider

  • All operations are adds (+): No child is ever disappointed.
  • All operations are subtracts (-) and initial ice cream is 0: Every child leaves disappointed.
  • Large values of n (e.g., 10⁵): The code handles them efficiently because each operation is O(1).
  • Input lines may contain leading spaces; getline() handles that appropriately when used carefully.

Time and Space Complexity

  • Time Complexity: O(n), as we process each of the n lines only once.
  • Space Complexity: O(1), excluding input string storage.

The program is efficient and easily handles the given constraints.

Common Mistakes to Avoid

  1. Mixing cin and getline:
    Be cautious when mixing cin and getline(); you might need an extra getline() to consume the newline after reading x.

  2. Not checking bounds:
    Always ensure that x >= d before subtracting to avoid incorrect logic.

  3. Incorrect string-to-int conversion:
    Some may try stoi(s.substr(1)), which works too, but in this code we manually parse digits to keep control over every character.

Related Problems to Practice

Here are some similar problems to improve your simulation and string processing skills:

These problems involve basic logic and simulation — great for beginners and intermediate programmers.

Conclusion

Codeforces 686A - Free Ice Cream is a classic simulation problem that tests how well you can handle conditional logic and input parsing. It's not about algorithms — it's about keeping track of variables and state changes efficiently.

Once you understand how to extract numbers from strings and manage counters and conditions, problems like these become quick wins in contests. They also help build a strong foundation for more complex simulations later on.

If you’re preparing for contests, practice more problems that involve event simulation and string parsing — they show up a lot more than you’d think.