자료구조 중 스택의 개념에 대해 익히고 있다면 쉽게 풀 수 있는 문제입니다.
만약 28278 - 스택 2 문제를 해결했다면 정말 쉽게 풀 수 있습니다.
#include <iostream>
int main()
{
int k;
std::cin >> k;
int* stack = new int[k];
for (int i = 0; i < k; i++)
stack[i] = -1;
int stack_top = -1;
for (int i = 0; i < k; i++)
{
int temp;
std::cin >> temp;
if (temp != 0)
{
stack_top++;
stack[stack_top] = temp;
}
else
{
if (stack_top != -1)
{
stack[stack_top] = -1;
stack_top--;
}
}
}
int result = 0;
for (int i = 0; i < k; i++)
if (stack[i] != -1)
result += stack[i];
std::cout << result;
return 0;
}