BOJ 1495 기타리스트

 
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <functional>

typedef long long int ll;
using namespace std;
#define INF 1234567890
#define N 200000
int dp[105][1005];
int value[105];

const int LIMIT = 1e+9;
int n, s, m;
ll powf(ll base, ll exp) {
	ll res = 1;
	while (exp) {
		if (exp & 1)
			res *= base;
		exp >>= 1;
		base *= base;
	}
	return res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n >> s >> m;
	for (int i = 1; i <= n; i++) {
		cin >> value[i];
	}
	dp[0][s] = 1;

	for (int i = 1; i <= n; i++) {
		int check = 0;
		for (int j = 0; j <= m; j++) {
			if (dp[i - 1][j]) {
				int l = j - value[i];
				int r = j + value[i];

				if (l >= 0 && l <= m) {
					dp[i][l] = 1;
					check = 1;
				}

				if (r >= 0 && r <= m) {
					dp[i][r] = 1;
					check = 1;
				}
			}
		}
		if (check == 0) {
			cout << -1;
			return 0;
		}
	}
	int res = 0;
	for (int i = 0; i <= m; i++) {
		if (dp[n][i]) {
			res = i;
		}
	}
	cout << res;

	return 0;
}