Skip to content

Instantly share code, notes, and snippets.

@luciocf
Created July 14, 2021 03:43
Comentário NOIC - OBI 2021 - Fase 1 P2 - Zero para cancelar
// Comentário NOIC - OBI 2021 - Fase 1 P2 - Zero para cancelar
// Lúcio Figueiredo
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int n;
scanf("%d", &n);
stack<int> stk; // declaramos a stack
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
if (x == 0) stk.pop();
else stk.push(x);
}
// resposta do problema
int ans = 0;
// calculamos a soma dos números que sobram
while (stk.size() > 0)
{
ans += stk.top();
stk.pop();
}
printf("%d\n", ans);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment