import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Integer> stack = new Stack<>();
int K = Integer.parseInt(br.readLine());
int result = 0;
for(int i=0; i<K; i++) {
int num = Integer.parseInt(br.readLine());
if(num == 0) {
stack.pop();
} else {
stack.push(num);
}
}
for(int n:stack) {
result += n;
}
System.out.println(result);
}
}