USACO Combination Lock

Notice that \(n\) may be smaller than 5.

Code

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
/*
ID: wcr19961
PROG: combo
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <set>
using namespace std;

typedef long long LL;
const int maxn = 5;
int a[maxn], b[maxn];
set<int> s[maxn];

int main() {
freopen("combo.in", "r", stdin);
freopen("combo.out", "w", stdout);
int n, ans = 1;
scanf("%d", &n);
scanf("%d%d%d", &a[0], &a[1], &a[2]);
scanf("%d%d%d", &b[0], &b[1], &b[2]);
for (int i = 0; i < 3; ++i) {
if (a[i] < b[i]) {
swap(a[i], b[i]);
}
ans *= max(0, 5 - min(a[i] - b[i], b[i] - a[i] + n));
}
printf("%d\n", n > 5 ? 250 - ans : n * n * n);
return 0;
}