forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrickwall.cpp
38 lines (35 loc) · 956 Bytes
/
brickwall.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
#include <bits/stdc++.h>
using namespace std;
const int N = 300*300+10;
int sum = 0;
bool isCrack[N], visited[301][301][301];
int n, c1, c2, c3;
bool dfs(int cnt1, int cnt2, int cnt3) {
int Dist = cnt1+2*cnt2+3*cnt3;
if(Dist == sum) return true;
if(Dist > sum) return false;
if(isCrack[Dist]) return false;
visited[cnt1][cnt2][cnt3] = true;
if(cnt1+1 <= c1 && !visited[cnt1+1][cnt2][cnt3] && dfs(cnt1+1, cnt2, cnt3)) {
return true;
}
if(cnt2+1 <= c2 && !visited[cnt1][cnt2+1][cnt3] && dfs(cnt1, cnt2+1, cnt3)) {
return true;
}
if(cnt3+1 <= c3 && !visited[cnt1][cnt2][cnt3+1] && dfs(cnt1, cnt2, cnt3+1)) {
return true;
}
return false;
}
int main() {
cin >> n >> c1 >> c2 >> c3;
for(int i = 0; i < n; ++i) {
int len;
cin >> len;
sum += len;
isCrack[sum] = true;
}
if(dfs(0,0,0)) {
cout << "YES\n";
} else cout << "NO\n";
}