We are given a problem: There are n people arriving in order, each with a rating from 0 to 10. Chandan, when encountering a rating of 0 (presumably after the ratings are given or as he processes them?), gets frustrated and removes the candidate who scored that 0 and also removes the candidate who came before him. If there is no candidate before (i.e., it's the first candidate), he does nothing. We need to compute the total sum of ratings of all remaining people after processing? The problem statement: "You've to find the summation of all the ratings in a day for Chandan." It says: "Chandan rates every candidate from 0 to 10. He has to output the total ratings of all the people who came in a day. But, here's the problem: Chandan gets extremely frustrated when someone ends up scoring a 0 in the interview. So in frustration he ends up removing the candidate who scored that 0, and also removes the candidate who came before him. If there is no candidate before the one who scores a 0, he does nothing. You've to find the summation of all the ratings in a day for Chandan."
Interpretation: We have an initial list of ratings for the n candidates. When a rating is 0, Chandan removes that candidate and the immediate previous candidate (if exists). After these removals, the total sum of ratings of the candidates that remain (or maybe the sum of ratings of all candidates originally, but after removal?) That is what we need.
But careful: The problem says: "He has to output the total ratings of all the people who came in a day." But then due to frustration he removes some. So presumably the final output is the sum of ratings of the people that remain after removal. That matches typical problems: Given a list, whenever you see a 0, delete the zero and the element before it (if any). Compute sum of remaining elements.
But need to consider order: The removal might affect subsequent removals? For example, if we have 2, 0, 3. According to rule: The second person scores 0, so we remove the second (0) and the first (2). Then only 3 remains, sum=3. But what about if there are multiple zeros? The removals might be cascading. For instance: Input: 1, 2, 0, 0, 3. How to process? The typical approach: process sequentially: When we see a 0, we remove that zero and the previous element (if exists) from the current list. But after removal, the indices shift. Many problems of this type treat the list as a stack: When we encounter a 0, we pop the previous element (if any) and skip the zero. This is similar to a "delete previous element when encountering zero". The sample: n=5, ratings: 2, 3, 0, 7, 0. Sample output: 2. Let's simulate.
List: [2,3,0,7,0]
Approach: Process from left to right? Suppose we process each rating as we read: maintain a list of survivors. Or treat as: We have a sequence, and whenever a zero appears, remove the zero and the element immediately before it (if it exists) from the current sequence. Then continue scanning from the next element after zero? But after removal, the elements after the zero shift left, and a new zero might be adjacent to an element that was previously before the removed ones? Possibly.
Let's attempt:
Initial: 2 3 0 7 0
We need to determine removal order. One interpretation: After the whole process (maybe after all removals are done, with multiple passes?), the sum is 2.
Maybe we can think as: The effect is like using a stack. For each rating, if it is 0, then if the stack is not empty, pop the top (the previous candidate), and also don't push the 0 (i.e., skip it). If rating is non-zero, push it onto the stack. Then the sum of stack is the answer. Let's test with sample:
Start with empty stack.
Read 2 (non-zero) -> push: stack=[2]
Read 3 -> push: stack=[2,3]
Read 0 -> Since 0, if stack not empty, pop top: pop 3; also discard 0. So stack becomes [2]
Read 7 -> non-zero, push: stack=[2,7]
Read 0 -> stack not empty, pop top: pop 7; discard 0. Stack=[2]
No more inputs. Sum of stack = 2. That matches sample output.
Thus the process is exactly: simulate a list where when we encounter a 0, we remove the most recently added non-zero candidate (i.e., the previous candidate) and also skip the 0. That is like a stack with a rule: 0 acts as a "delete previous element" operation.
The problem constraints: n up to 5000, small. Could be O(n). The stack approach works.
But we need to confirm with more scenarios: What if there is a zero when stack empty? Then do nothing (skip the zero). That's consistent.
Also, if there are consecutive zeros? For example: ratings: 1,0,0. Process:
- push 1.
- read 0: pop 1 (stack empty), discard 0.
- read 0: stack empty, do nothing.
Result sum=0.
Alternatively, if we considered removal after all zeros processed in order, but the stack approach yields consistent results.
Thus solution: Read n, then for i from 1 to n: read rating r. If r==0: if stack not empty, pop. Else, if r!=0, push r. At the end, compute sum of elements in stack.
Implementation: Use an array as stack or list. Complexity O(n).
Now double-check: The sample input has 2 zeros; stack process yields 2, correct.
Edge cases: n=1, rating=0 -> stack empty -> sum=0. If rating=5 -> push -> sum=5.
Also, test case: 2,0,2 => after: push2, read0 pop2, push2 => sum=2. That seems plausible.
Now writing solution in Python.
Note: Input constraints small, but Python fine.
We should produce final answer: print the sum.
I'll include explanation in solution: We simulate reading ratings, using a list as stack. If rating >0 push, if rating ==0 and stack not empty, pop.
Then compute sum(stack).
Let's code.
But wait: The problem says "removing the candidate who scored that 0, and also removes the candidate who came before him." It doesn't say that removal only applies to the immediate predecessor; but by "the candidate who came before him" it's logical it's the immediate previous candidate. Also the stack approach matches that because we only have a linear order, and we are processing sequentially, so the last added candidate is the one just before the zero.
Thus correct.
Now test with sample: Input:
5
2
3
0
7
0 -> our code outputs 2.
Also, note: The ratings can be from 0 to 10 inclusive. So 0 is only considered for removal; positive ratings are normal.
All good.
Now produce final answer.
But wait: The problem might also be interpreted that the removal happens after all ratings are collected and then we remove zeros and previous ones from the original list, but that could be ambiguous with respect to overlapping removals. However, the stack approach is typical for such problems and matches sample.
Let's test with another custom case: Input: 4, ratings: 0,1,2,0. If we use stack:
- read 0: stack empty, ignore.
- read 1: push -> [1]
- read 2: push -> [1,2]
- read 0: pop top -> pop 2, stack [1]
Sum=1.
If we instead remove from original list: list [0,1,2,0]. Process zeros in order. First zero at index0: no previous, do nothing. Second zero at index3: previous index2 is 2, remove both. Remaining [1]. Sum=1. Same.
But consider overlapping: [1,0,0]. Original list: [1,0,0]. Process sequentially: First zero at index1: remove it and previous (index0) => list becomes []? After removal, indices change; second zero originally index2 now would be at index0? But if we process after removal, we might have to re-evaluate. Let's test both interpretations:
Stack: push1 -> [1]; read0 -> pop1, stack[]; read0 -> stack empty, ignore. Sum=0.
Now, if we remove from original list but in a single pass without adjusting: i.e., mark removal of zeros and their predecessors; but if predecessor is already removed by another zero, maybe we need to decide. For [1,0,0]: Zero at index1 removes index1 and index0. Then zero at index2 originally, but after removal of first two, what's left? The list becomes [] (since only element was index2? Actually, original: indices:0:1,1:0,2:0. After removing indices0 and1, we have only index2:0. But that zero is now alone. But the rule says: when someone ends up scoring a 0, he removes that candidate and the candidate who came before him. If there is no candidate before, he does nothing. In the resulting list after first removal, there is a zero with no previous candidate? But the order of processing matters. If we process zeros in the order they appear in the original sequence, after removing 0 and 1, the second zero (original index2) is now at position 0 in the remaining list? But it originally had a candidate before it (index1), but that candidate got removed. Does that count? The typical rule in such problems (like "Backspace" or "delete") is to process the list sequentially and apply the rule at the moment you see a zero. That is the stack approach. So it's consistent.
If we did a different approach: remove all zeros and their immediate predecessors simultaneously from the original list, but if a predecessor is also a zero and gets removed by another zero, we might get conflicts. For [1,0,0], if we remove each zero and its predecessor: first zero (index1) removes index1 and index0. second zero (index2) would remove index2 and index1 (but index1 already removed) and index1 may be considered? Also index2's predecessor originally was index1 which is removed, but then there might be no predecessor, so second zero does nothing? Or maybe we treat the original list without removal effects: index2's predecessor is index1, even if we remove it, we still consider it? But then we would remove index2 and index1, but index1 already removed (maybe double counting). That yields remaining possibly []? That sum=0 as well. So ambiguous.
But most likely the intended solution is the stack simulation, common for such problems (like "delete and backspace").
Given sample matches stack, we adopt it.
Now we need to write solution.
Let's produce code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
ratings = list(map(int, data[1:1+n]))
stack = []
for r in ratings:
if r == 0:
if stack:
stack.pop()
else:
stack.append(r)
print(sum(stack))
That's it.
Complexity: O(n) time, O(n) space (worst case stack size up to n).
Alternatively, we could also use a variable sum and a count of survivors, but because we need to pop, we need to know the last added rating. So stack is simplest.
Now produce final answer.
We'll include explanation.
But the problem maybe expects simple simulation with array and pointer.
All good.
Now finalize. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
ratings = list(map(int, data[1:1 + n]))
stack = []
for r in ratings:
if r == 0:
if stack: # there is a previous candidate
stack.pop() # remove that candidate
# the zero itself is never added
else:
stack.append(r)
print(sum(stack))
if name == "main":
solve()