백준 2579 c++
#include <iostream>
#include <algorithm>
using namespace std;
int input(int lower, int upper)
{
//cout << "input()" << endl;
int A;
while (1)
{
cin >> A;
if (A >= lower && A <= upper)
{
break;
}
else
{
;
}
}
return A;
}
void input_stair(int* stair, int n)
{
//cout << "input_stair()\n";
int i;
for (i = 1; i <= n; i++)
{
stair[i] = input(1, 10000);
}
return;
}
int find_result(int* stair, int n)
{
//cout << "find_result()";
int i;
int* result = new int[n+3];//버퍼 오버런 : n값이 1,2일경우 n+1이 3이 되지 못할 수 있음
int maximum;
result[0] = stair[0];
result[1] = stair[0] + stair[1];
result[2] = stair[0] + stair[1] + stair[2];
result[3] = max(stair[2] + stair[3], stair[1] + stair[3]);
for (i = 4; i <= n; i++)
{
result[i] = max(result[i-3] + stair[i-1], result[i-2]) + stair[i];
}
maximum = result[n];
delete[] result;
return maximum;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int* stair;
int n;
n = input(1, 300);
stair = new int[n + 1] {0};
input_stair(stair, n);
cout << find_result(stair, n) << "\n";
delete[] stair;
return 0;
}