오랜만에 easy~ 가볍게 재귀를 사용하면 된다. (나는 재귀를 사용할때 함수를 항상 DFS라고 쓴다.. 조심하시길!)
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int ans = -1;
pair<int,int> d[16];
void DFS(int L, int sum) {
if (L==n+1) {
if (ans == -1 || ans < sum) ans = sum;
}
else {
if (d[L].first + L <= n+1) DFS(L + d[L].first, sum + d[L].second);
DFS(L + 1, sum);
}
}
int main() {
//freopen("in1.txt", "rt", stdin);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> d[i].first >> d[i].second;
}
DFS(1, 0);
cout << ans << '\n';
return 0;
}