문제 확인하기
2. 정답 코드 (C++17, memory: 2020KB, time: 4ms)
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n;
std::cin >> n;
std::vector<int> array(10000, 0);
for (int i = 0; i < n; ++i) {
std::cin >> array[i];
}
std::vector<int> d(10000, 0);
d[0] = array[0];
d[1] = array[0] + array[1];
d[2] = std::max({array[2] + array[0], array[2] + array[1], d[1]});
for (int i = 3; i < n; ++i) {
d[i] = std::max({array[i] + d[i - 2], array[i] + array[i - 1] + d[i - 3], d[i - 1]});
}
std::cout << *std::max_element(d.begin(), d.begin() + n) << std::endl;
return 0;
}