{ }codelper
break / continueP0223

Loading a delivery truck in order

Boxes are loaded onto a delivery truck in a fixed order. The first line gives the number of boxes n, the total weight C the truck can carry, and the weight limit L for a single box, separated by spaces. The second line gives the weights of the n boxes in loading order, separated by spaces.
The boxes are numbered from 1 in this order. Look at the boxes one by one from box 1 and apply the following rules from the top. If a box weighs more than L, do not load it: send it separately and move on to the next box. Otherwise, if loading the box would make the total loaded weight greater than C, stop at that box. After stopping, that box and every box after it are neither loaded nor sent separately, even if lighter boxes come later. If neither applies, load the box.
The total may be exactly C after loading. Print on one line the number of loaded boxes, the total loaded weight, the number of boxes sent separately and the number of the box where loading stopped, separated by single spaces. If loading never stopped through the last box, print 0 in place of the stopping box number.

Input
1 ≤ n ≤ 10,000, 1 ≤ L ≤ C ≤ 1,000,000, 1 ≤ box weight ≤ 1,000,000.
All integers
Output
One line: loaded boxes, total loaded weight, boxes sent separately, stopping box number (0 if never stopped)
Example 1 · stdin
6 15 8
5 9 6 7 2 1
stdout
2 11 1 4
Example 2 · stdin
5 10 5
4 4 9 1 3
stdout
3 9 1 5
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Skipping just this box is continue, and ending the whole loop is break.
Python, JS, TS, Java, C#, C and C++ all use the same words, and Craykon uses continue and break too (건너뛰기 and 그만 in Korean).
Swapping them is the most common mistake.
Using continue when the truck would overflow keeps loading the lighter boxes that come later, and using break on a heavy box never looks at the boxes after it.
The order of the rules matters too.
If you check for overflow first, you stop at a heavy box that arrives where it would overflow, but that box must be sent separately.
A total of exactly C is still loaded, so ask >, not >=.
break ends only the innermost loop, and inside a switch in C, C++, Java, JS, TS and C# it leaves only the switch (the same goes for break inside a Craykon switch).
Instead of a separate stop number you could use Python's for … else (the else runs only when the loop finished without break).
If your loop counts from 0, add 1 when printing the box number.

Pythonmain.py · 14 lines
n, cap, limit = map(int, input().split())boxes = list(map(int, input().split()))loaded = total = sent = 0stop = 0for i, w in enumerate(boxes, start=1):    if w > limit:          # 너무 무거운 상자: 따로 보내고 다음 상자로        sent += 1        continue    if total + w > cap:    # 실으면 넘친다: 여기서 아예 멈춘다        stop = i        break    loaded += 1    total += wprint(loaded, total, sent, stop)
JavaScriptmain.js · 19 lines
const [n, cap, limit, ...boxes] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let loaded = 0;let total = 0;let sent = 0;let stop = 0;for (let i = 0; i < n; i++) {  const w = boxes[i];  if (w > limit) {    sent++;    continue; // 이 상자만 건너뛴다  }  if (total + w > cap) {    stop = i + 1;    break; // 반복 전체를 끝낸다  }  loaded++;  total += w;}console.log(`${loaded} ${total} ${sent} ${stop}`);
TypeScriptmain.ts · 20 lines
import { readFileSync } from "node:fs";const [n, cap, limit, ...boxes]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let loaded = 0;let total = 0;let sent = 0;let stop = 0;for (let i = 0; i < n; i++) {  const w: number = boxes[i];  if (w > limit) {    sent++;    continue; // 이 상자만 건너뛴다  }  if (total + w > cap) {    stop = i + 1;    break; // 반복 전체를 끝낸다  }  loaded++;  total += w;}console.log(`${loaded} ${total} ${sent} ${stop}`);
JavaMain.java · 30 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer; public class Main {    public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        StringTokenizer st = new StringTokenizer(in.readLine());        int n = Integer.parseInt(st.nextToken());        int cap = Integer.parseInt(st.nextToken());        int limit = Integer.parseInt(st.nextToken());        st = new StringTokenizer(in.readLine());        int loaded = 0, total = 0, sent = 0, stop = 0;        for (int i = 1; i <= n; i++) {            int w = Integer.parseInt(st.nextToken());            if (w > limit) {                sent++;                continue; // 이 상자만 건너뛴다            }            if (total + w > cap) {                stop = i;                break; // 반복 전체를 끝낸다(남은 수는 읽지 않아도 된다)            }            loaded++;            total += w;        }        System.out.println(loaded + " " + total + " " + sent + " " + stop);    }}
C#Main.cs · 30 lines
using System;using System.Linq; class Program{    static void Main()    {        int[] first = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();        int n = first[0], cap = first[1], limit = first[2];        int[] boxes = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();        int loaded = 0, total = 0, sent = 0, stop = 0;        for (int i = 0; i < n; i++)        {            int w = boxes[i];            if (w > limit)            {                sent++;                continue; // 이 상자만 건너뛴다            }            if (total + w > cap)            {                stop = i + 1;                break; // 반복 전체를 끝낸다            }            loaded++;            total += w;        }        Console.WriteLine($"{loaded} {total} {sent} {stop}");    }}
C++main.cpp · 25 lines
#include <iostream>#include <vector> int main() {    std::ios::sync_with_stdio(false);    int n, cap, limit;    std::cin >> n >> cap >> limit;    std::vector<int> boxes(n);    for (int& w : boxes) std::cin >> w;    int loaded = 0, total = 0, sent = 0, stop = 0;    for (int i = 0; i < n; i++) {        int w = boxes[i];        if (w > limit) {            sent++;            continue;  // 이 상자만 건너뛴다        }        if (total + w > cap) {            stop = i + 1;            break;  // 반복 전체를 끝낸다        }        loaded++;        total += w;    }    std::cout << loaded << " " << total << " " << sent << " " << stop << "\n";}
Cmain.c · 22 lines
#include <stdio.h> int main(void) {    int n, cap, limit, w;    int loaded = 0, total = 0, sent = 0, stop = 0;    scanf("%d %d %d", &n, &cap, &limit);    for (int i = 1; i <= n; i++) {        scanf("%d", &w);        if (w > limit) {            sent++;            continue; /* 이 상자만 건너뛴다 */        }        if (total + w > cap) {            stop = i;            break; /* 반복 전체를 끝낸다 */        }        loaded++;        total += w;    }    printf("%d %d %d %d\n", loaded, total, sent, stop);    return 0;}
Craykonmain.cray · 26 lines
first = Console.ReadNumbers();n = first[0];cap = first[1];limit = first[2];boxes = Console.ReadNumbers();loaded = 0;total = 0;sent = 0;stop = 0;for (i = 0; i < n; i++){    w = boxes[i];    if (w > limit)    {        sent++;        continue; // 이 상자만 건너뛴다    }    if (total + w > cap)    {        stop = i + 1;        break; // 반복 전체를 끝낸다    }    loaded++;    total += w;}Console.WriteLine(loaded + " " + total + " " + sent + " " + stop);