/
BOJ1495.cpp
62 lines (42 loc) · 831 Bytes
/
BOJ1495.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
#define MAX_N 100
#define MAX_M 1000
#define INF 20000000
int N, S, M;
int cache[MAX_N + 1][MAX_M + 1];
int input[MAX_N + 1];
int solve(int n ,int v)
{
if (n == N)
{
return v;
}
int &ret = cache[n][v];
if (ret != -1)
return ret;
ret = -INF;
if (v - input[n] >= 0)
ret = max(ret, solve(n+1, v - input[n]));
if (v + input[n] <= M)
ret = max(ret, solve(n+1, v + input[n]));
return ret;
}
int main(int argc, char** argv)
{
std::ios::sync_with_stdio(false);
#ifdef _WIN32
freopen("Text.txt", "r", stdin);
#endif
memset(cache, -1, sizeof(cache));
cin >> N >> S >> M;
for (int i = 0; i<N; i++)
{
cin >> input[i];
}
cout << (solve(0, S) <= -INF ? -1 : solve(0,S));
return 0;
}