-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutribbon.java
More file actions
408 lines (345 loc) · 11.1 KB
/
cutribbon.java
File metadata and controls
408 lines (345 loc) · 11.1 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import static java.lang.System.out;
public class cutribbon {
private static int answer= 0;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int n = sc.nextInt();
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
Integer[] dp = new Integer[n+1];
int aa= dfs(n,a,b, c,dp);
out.println(aa);
}
private static int dfs(int n, int a, int b, int c, Integer[] dp) {
if(n < 0) return Integer.MIN_VALUE;
if(n == 0){
return 0;
}
if(dp[n]!=null) return dp[n];
int first = dfs(n - a, a,b,c, dp);
int second = dfs(n - b, a,b,c, dp);
int third = dfs(n - c, a,b,c, dp);
return dp[n] =Math.max(first, Math.max(second, third)) + 1;
}
private static int allFourMatrix(char[][] arr, int i, int j) {
int n = arr.length;
int left = (i - 1) < 0 ? 0 : (arr[i - 1][j] == 'o' ? 1 : 0);
int right = (i + 1) >= n ? 0 : (arr[i + 1][j] == 'o' ? 1 : 0);
int up = (j - 1) < 0 ? 0 : (arr[i][j - 1] == 'o' ? 1 : 0);
int down = (j + 1) >= n ? 0 : (arr[i][j + 1] == 'o' ? 1 : 0);
return left + right + up + down;
}
private static int[] readarr(FastScanner sc, int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
return arr;
}
private static long[] readarr1(FastScanner sc, int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
return arr;
}
private static void readarr(int[] arr, FastScanner sc, int n) {
}
static long Ceil(long a, long b) {
if (a % b != 0) {
return (a / b) + 1;
} else {
return (a / b);
}
}
static int intCeil(int a, int b) {
if (a % b != 0) {
return (a / b) + 1;
} else {
return (a / b);
}
}
private static void input(int[] arr, int n, FastScanner sc) {
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
}
private static void no() {
out.println("No");
}
private static void yes() {
out.println("Yes");
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception {
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for (int i = 0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
public static void print(int[] arr) {
//for debugging only
for (int x : arr)
out.print(x + " ");
out.println();
}
public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception {
long[] arr = new long[N];
st = new StringTokenizer(infile.readLine());
for (int i = 0; i < N; i++)
arr[i] = Long.parseLong(st.nextToken());
return arr;
}
public static boolean isPrime(long n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) return false;
}
return true;
}
public static long gcd(long a, long b) {
if (a > b)
a = (a + b) - (b = a);
if (a == 0L)
return b;
return gcd(b % a, a);
}
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
public static long totient(long n) {
long result = n;
for (int p = 2; p * p <= n; ++p)
if (n % p == 0) {
while (n % p == 0)
n /= p;
result -= result / p;
}
if (n > 1)
result -= result / n;
return result;
/*
find phi(i) from 1 to N fast
O(N*loglogN)
long[] arr = new long[N+1];
for(int i=1; i <= N; i++)
arr[i] = i;
for(int v=2; v <= N; v++)
if(arr[v] == v)
for(int a=v; a <= N; a+=v)
arr[a] -= arr[a]/v;
*/
}
public static ArrayList<Integer> findDiv(int N) {
//gens all divisors of N
ArrayList<Integer> ls1 = new ArrayList<Integer>();
ArrayList<Integer> ls2 = new ArrayList<Integer>();
for (int i = 1; i <= (int) (Math.sqrt(N) + 0.00000001); i++)
if (N % i == 0) {
ls1.add(i);
ls2.add(N / i);
}
Collections.reverse(ls2);
for (int b : ls2)
if (b != ls1.get(ls1.size() - 1))
ls1.add(b);
return ls1;
}
public static void sort(int[] arr) {
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
for (int i = 0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long power(long x, long y, long p) {
//0^0 = 1
long res = 1L;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res;
}
//custom multiset (replace with HashMap if needed)
public static void push(TreeMap<Integer, Integer> map, int k, int v) {
//map[k] += v;
if (!map.containsKey(k))
map.put(k, v);
else
map.put(k, map.get(k) + v);
}
public static void pull(TreeMap<Integer, Integer> map, int k, int v) {
//assumes map[k] >= v
//map[k] -= v
int lol = map.get(k);
if (lol == v)
map.remove(k);
else
map.put(k, lol - v);
}
public static int[] compress(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int boof = 1; //min value
for (int x : ls)
if (!map.containsKey(x))
map.put(x, boof++);
int[] brr = new int[arr.length];
for (int i = 0; i < arr.length; i++)
brr[i] = map.get(arr[i]);
return brr;
}
//****CLASS PAIR ************************************************
private static class Pair implements Comparable<Pair> {
int src;
long a;
long b;
Pair(int src, long a, long b) {
this.src = src;
this.a = a;
this.b = b;
}
public int compareTo(Pair o) {
return (int) (this.b - o.b);
}
}
//****CLASS PAIR **************************************************
public static long[][] multiply(long[][] left, long[][] right) {
long MOD = 1000000007L;
int N = left.length;
int M = right[0].length;
long[][] res = new long[N][M];
for (int a = 0; a < N; a++)
for (int b = 0; b < M; b++)
for (int c = 0; c < left[0].length; c++) {
res[a][b] += (left[a][c] * right[c][b]) % MOD;
if (res[a][b] >= MOD)
res[a][b] -= MOD;
}
return res;
}
public static long[][] power(long[][] grid, long pow) {
long[][] res = new long[grid.length][grid[0].length];
for (int i = 0; i < res.length; i++)
res[i][i] = 1L;
long[][] curr = grid.clone();
while (pow > 0) {
if ((pow & 1L) == 1L)
res = multiply(curr, res);
pow >>= 1;
curr = multiply(curr, curr);
}
return res;
}
private static class FastScanner {
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
}