-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmusementPark
More file actions
91 lines (82 loc) · 2.05 KB
/
AmusementPark
File metadata and controls
91 lines (82 loc) · 2.05 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Sol();
}
}
class Sol{
long N, M;
private final Buf buf = new Buf();
private final List<Long> input = new ArrayList<>();
public Sol(){
N = buf.readLong();
M = buf.readLong();
long max = 0;
for (int i = 0; i<M; i++){
input.add(buf.readLong());
max = Math.max(input.get(i), max);
}
if (N<=M){
System.out.println(N);
return;
}
long right = max *N;
long left = 0;
while(left<right){
long mid = (right+left)/2;
if (inCan(mid)){
right = mid;
}else{
left = mid+1;
}
}
long tmp = M;
for (int i = 0; i<M; i++){
tmp += left/input.get(i);
}
tmp -= N;
long result = 0;
for (int i = (int) (M-1); 0<=i; i--){
if (left%input.get(i)==0){
if (tmp == 0){
result = i+1;
break;
}
tmp --;
}
}
System.out.println(result);
}
public boolean inCan(long mid){
long tmp = M;
for (int i = 0; i<M; i++){
tmp += mid/input.get(i);
}
return N <= tmp;
}
}
class Buf{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
public String read(){
while(st==null||!st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch(IOException e){
return null;
}
}
return st.nextToken();
}
public Integer readInt(){
return Integer.parseInt(read());
}
public Long readLong(){
return Long.parseLong(read());
}
}