시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 256 MB 1296 950 834 73.351%

문제

나코더 기장 재민이는 동아리 회식을 준비하기 위해서 장부를 관리하는 중이다.

재현이는 재민이를 도와서 돈을 관리하는 중인데, 애석하게도 항상 정신없는 재현이는 돈을 실수로 잘못 부르는 사고를 치기 일쑤였다.

재현이는 잘못된 수를 부를 때마다 0을 외쳐서, 가장 최근에 재민이가 쓴 수를 지우게 시킨다.

재민이는 이렇게 모든 수를 받아 적은 후 그 수의 합을 알고 싶어 한다. 재민이를 도와주자!

입력

첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000)

이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경우 해당 수를 쓴다.

정수가 "0"일 경우에 지울 수 있는 수가 있음을 보장할 수 있다.

출력

재민이가 최종적으로 적어 낸 수의 합을 출력한다.

예제 입력 1

4
3
0
4
0

예제 출력 1

0

예제 입력 2

10
1
3
5
4
0
0
7
0
0
6

예제 출력 2

7

힌트

예제 2의 경우를 시뮬레이션 해보면,

  • [1]
  • [1,3]
  • [1,3,5]
  • [1,3,5,4]
  • [1,3,5] (0을 불렀기 때문에 최근의 수를 지운다)
  • [1,3] (0을 불렀기 때문에 그 다음 최근의 수를 지운다)
  • [1,3,7]
  • [1,3] (0을 불렀기 때문에 최근의 수를 지운다)
  • [1] (0을 불렀기 때문에 그 다음 최근의 수를 지운다)
  • [1,6]

합은 7이다.

W3sicHJvYmxlbV9pZCI6IjEwNzczIiwicHJvYmxlbV9sYW5nIjoiMCIsInRpdGxlIjoiXHVjODFjXHViODVjIiwiZGVzY3JpcHRpb24iOiI8cD5cdWIwOThcdWNmNTRcdWIzNTQgXHVhZTMwXHVjN2E1IFx1YzdhY1x1YmJmY1x1Yzc3NFx1YjI5NCBcdWIzZDlcdWM1NDRcdWI5YWMgXHVkNjhjXHVjMmRkXH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m9ncmFtIHRoYXQgcmVhZHMgdGhlIHNlcXVlbmNlIG9mIGJvc3Mgc3RhdGVtZW50cyBhbmQgY29tcHV0ZXMgdGhlIGNvcnJlY3Qgc3VtLjxcL3A+XHJcbiIsImlucHV0IjoiPHA+VGhlIGZpcnN0IGxpbmUgb2YgaW5wdXQgY29udGFpbnMgdGhlIGludGVnZXIgSyAoMSAmbGU7IEsgJmxlOyAxMDAgMDAwKSB3aGljaCBpcyB0aGUgbnVtYmVyIG9mIGludGVnZXJzIChpbmNsdWRpbmcgJmxkcXVvO3plcm8mcmRxdW87KSB5b3VyIGJvc3Mgd2lsbCBzYXkuIE9uIGVhY2ggb2YgdGhlIG5leHQgSyBsaW5lcywgdGhlcmUgd2lsbCBlaXRoZXIgYmUgb25lIGludGVnZXIgYmV0d2VlbiAxIGFuZCAxMDAgKGluY2x1c2l2ZSksIG9yIHRoZSBpbnRlZ2VyIDAuPFwvcD5cclxuIiwib3V0cHV0IjoiPHA+VGhlIG91dHB1dCBpcyBvbmUgbGluZSwgY29udGFpbmluZyB0aGUgaW50ZWdlciB3aGljaCBpcyB0aGUgY29ycmVjdCBzdW0gb2YgdGhlIGludGVnZXJzIHJlYWQsIHRha2luZyB0aGUgJmxkcXVvO3plcm8mcmRxdW87IHN0YXRlbWVudHMgaW50byBjb25zaWRlcmF0aW9uLiBZb3UgY2FuIGFzc3VtZSB0aGF0IHRoZSBvdXRwdXQgd2lsbCBiZSBhbiBpbnRlZ2VyIGluIHRoZSByYW5nZSAwIGFuZCAxIDAwMCAwMDAgKGluY2x1c2l2ZSkuPFwvcD5cclxuIiwiaGludCI6IiIsIm9yaWdpbmFsIjoiMSIsInByb2JsZW1fbGFuZ19jb2RlIjoiXHVjNjAxXHVjNWI0In1d

출처

Olympiad > Canadian Computing Competition & Olympiad > 2015 > CCC 2015 Senior Division 1번

  • 문제의 오타를 찾은 사람: busyhuman
  • 문제를 번역한 사람: koosaga