Skip to content

Instantly share code, notes, and snippets.

@dalcon10028
Created December 31, 2019 16:35
Show Gist options
  • Save dalcon10028/76e23bc97e7d411e1c4d5d3eb85b93f4 to your computer and use it in GitHub Desktop.
Save dalcon10028/76e23bc97e7d411e1c4d5d3eb85b93f4 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int K = sc.nextInt();
Stack<Integer> stack = new Stack<>();
for(int i=0; i<K; i++){
int input = sc.nextInt();
if(input==0) stack.pop();
else stack.push(input);
}
sc.close();
int sum = 0;
for (Integer i : stack)
sum+=i;
System.out.print(sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment