import java.util.LinkedList;
import java.util.Queue;
class Solution {
public int solution(int bridge_length, int weight, int[] truck_weights) {
int time = 0;
int sum = 0;
Queue<Integer> bridge = new LinkedList<Integer>();
Queue<Integer> wait = new LinkedList<Integer>();
for (int w : truck_weights) {
wait.offer(w);
}
for (int i = 0; i < bridge_length; i++) {
bridge.offer(0);
}
while (true) {
time++;
sum -= bridge.poll();
if (!wait.isEmpty() && sum + wait.peek() <= weight) {
bridge.offer(wait.peek());
sum += wait.peek();
wait.poll();
}
else {
bridge.offer(0);
}
if (sum == 0) break;
}
return time;
}
}