[BOJ] 1495 기타리스트

Time Lapse :None

1495.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
#include <iostream>

using namespace std;

#define endl '\n'

int N,S,M;
bool dp[1001];
int arr[101];

int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>N>>S>>M;
for(int i=0;i<N;i++)
cin>>arr[i];

dp[S]=true;
for(int i=0;i<N;i++)
{
bool choose[1001] = {false,};
for(int j=0;j<=M;j++)
{
if(dp[j])
{
if(j+arr[i]<=M)
choose[j+arr[i]]=true;
if(j-arr[i]>=0)
choose[j-arr[i]]=true;
}
}
for(int j=0;j<=M;j++)
dp[j]=choose[j];
}
for(int i=M;i>=0;i--)
if(dp[i])
{
cout<<i<<endl;
return 0;
}
cout<<"-1"<<endl;
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/1495/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.