CCC '15 S1 - Zero That Out

View as PDF

Submit solution

Points: 3
Time limit: 5.0s
Memory limit: 256M

Problem type
Canadian Computing Competition: 2015 Stage 1, Senior #1

Your boss has asked you to add up a sequence of positive numbers to determine how much money your company made last year.

Unfortunately, your boss reads out numbers incorrectly from time to time.

Fortunately, your boss realizes when an incorrect number is read and says "zero", meaning "ignore the current last number."

Unfortunately, your boss can make repeated mistakes, and says "zero" for each mistake.

For example, your boss can say "One, three, five, four, zero, zero, seven, zero, zero, six", which means the total is 7 as explained in the following chart.

Boss statement(s) Current numbers Explanation
"One, three, five, four" 1, 3, 5, 4 Record the first four numbers.
"zero, zero" 1, 3 Ignore the last two numbers.
"seven" 1, 3, 7 Record the number 7 at the end of our list.
"zero, zero" 1 Ignore the last two numbers.
"six" 1, 6 We have read all numbers, and the total is 7.

At any point, your boss will have said at least as many positive numbers as "zero" statements. If all positive numbers have been ignored, the sum is zero.

Write a program that reads the sequence of boss statements and computes the correct sum.

Input Specification

The first line of input contains the integer K (1 \le K \le 100\,000) which is the number of integers (including "zero") your boss will say. On each of the K lines, there will either be one integer between 1 and 100 (inclusive) or the integer 0.

Output Specification

The output is one line, containing the integer which is the correct sum of the integers read, taking the "zero" statements into consideration. You can assume that the output will be an integer in the range 0 and 1\,000\,000 (inclusive).

Sample Input 1


Sample Output 1


Sample Input 2


Sample Output 2



  • 2
    LeonLiur  commented on July 25, 2020, 12:38 p.m. edited

    Could someone take a look at my code? I don't know where the problem is, but I keep getting WA on test cases 1 and 3, I am using java 8. Thanks in advance!

    • 2
      maxcruickshanks  commented on July 25, 2020, 2:37 p.m.

      In your if-statement checking for zero, you have an unnecessary check that is causing you to WA (hint: you can completely remove isZero).

      • 1
        LeonLiur  commented on July 25, 2020, 7:47 p.m.

        I fixed if thanks to your comment, thanks a lot!

  • -7
    Orion222  commented on July 7, 2020, 2:08 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.

  • 0
    Charles232  commented on March 9, 2019, 11:01 a.m.

    Will 0 ever be the 1st number?

    • 6
      geese  commented on March 9, 2019, 10:18 p.m.

      No, the Boss doesn't have any possible mistakes to erase yet. The Boss's power is only able to skip back up to the amount of positive numbers given. It just works.

  • -3
    Arihan10  commented on Jan. 26, 2019, 10:18 p.m.

    I'm getting TLEing on this problem. What should I do now? Thanks.

  • 6
    Roronoa_Zoro1540  commented on Aug. 10, 2017, 4:50 p.m.

    np fam

  • 11
    Roronoa_Zoro1540  commented on Aug. 10, 2017, 12:13 p.m.

    it says "One, five, three, four, zero, zero, seven, zero, zero, six" on the top but in the chart it says "One, three, five, four"

    • 8
      Phoenix1369  commented on Aug. 10, 2017, 2:42 p.m.

      Fixed, thanks!

  • 9
    Zezima  commented on Nov. 27, 2015, 10:55 a.m.

    There's a typo: "...many positiven umbers as..." should be changed to "...many positive numbers as..."

    • 7
      Xyene  commented on Nov. 28, 2015, 2:44 a.m.

      Fixed, thanks!