forked from edinfazlic/CodinGame-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RollerCoaster.java
35 lines (30 loc) · 872 Bytes
/
RollerCoaster.java
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
import java.util.Scanner;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int attractionSize = in.nextInt();
int C = in.nextInt();
int numberOfGroups = in.nextInt();
int[] P = new int[numberOfGroups];
for (int i = 0; i < numberOfGroups; i++) {
P[i] = in.nextInt(); // group size
}
int queueStart = 0;
long earnings = 0;
for(int i = 0; i < C; i++) {
int currentCapacity = 0;
int currentGroup = queueStart;
boolean fullCircle = false;
while(P[currentGroup] + currentCapacity <= attractionSize && !(fullCircle && currentGroup == queueStart)) {
currentCapacity += P[currentGroup];
if(++currentGroup == numberOfGroups) {
currentGroup = 0;
fullCircle = true;
}
}
queueStart = currentGroup;
earnings += currentCapacity;
}
System.out.println(earnings);
}
}