#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void spop(void);
void spush(int s);
int* stack;
int top = -1;
int main() {
int n, sum = 0;
scanf("%d", &n);
int s;
stack = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n;i++) {
scanf("%d", &s);
if (s == 0) {
spop();
}
else {
spush(s);
}
}
for (int i = 0; i <= top; i++) {
sum = sum + stack[i];
}
printf("%d", sum);
free(stack);
}
void spush(int s) {
top++;
stack[top] = s;
}
void spop(void) {
stack[top] = 0;
top--;
}