A courier company sets its fee from a box's weight and the region it is sent to.
The first line gives the number k of remote regions such as islands and mountain areas, and the second line gives the k remote region numbers, separated by spaces.
The third line gives the number of boxes n, and each of the next n lines gives one box's destination region number, weight (in grams) and service type, separated by spaces.
The service type is normal delivery "N" or express delivery "X".
The base fee depends on the weight.
Up to 1,000 g it is 3,500 won; over 1,000 g up to 3,000 g, 4,000 won; over 3,000 g up to 7,000 g, 5,000 won.
Over 7,000 g up to 15,000 g it is 6,500 won; over 15,000 g up to 25,000 g, 8,500 won.
Boxes over 25,000 g are not accepted.
If the destination region number equals one of the remote region numbers, add 3,000 won.
Express delivery is possible only if the destination is not a remote region and the weight is at most 15,000 g, and then 2,000 won is added.
A box whose express delivery is not possible is not accepted either.
Print the fee of each box, one per line in input order, and print "reject" for a box that is not accepted.
On the last line print the number of accepted boxes and the sum of their fees, separated by one space.
Input
1 ≤ k ≤ 100, 1 ≤ n ≤ 1,000, region numbers are integers from 1 to 999 (no leading zeros) and the remote region numbers are all different, 1 ≤ weight ≤ 40,000
Output
For each box its fee or "reject", then the number of accepted boxes and the sum of their fees
Example 1 · stdin
3
12 305 7
7
12 1000 N
112 1001 N
305 3000 X
7 25000 N
50 15000 X
50 15001 X
9 25001 N
stdout
6500
4000
reject
11500
8500
reject
reject
4 30500
main.py
Error List00F8 ↓ · ⇧F8 ↑
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
The first trap is the membership test that asks whether a region is remote. If you keep the second line as a string and search it with Python "12" in line, JS/TS line.includes("12"), Java line.contains("12"), C# line.Contains("12") or C strstr, you are searching for a substring, so the "12" inside "112" is found too. Convert the numbers and collect them, then ask with in on a Python set, JS/TS Set.has, Java HashSet.contains, C# HashSet<int>.Contains or C++ unordered_set::count (contains in C++20). C has no set, but the numbers are 1 to 999, so one table is_remote[number] is the set. In Craykon, use List.Contains(r) on the number list read with Console.ReadNumbers(). A number in the list is not equal to a string cut from a line ("12"), so match them with ToNumber(). The second trap is the weight bands. An else-if chain runs only the first branch that is true, from the top, so if you ask from the smallest band with <= and join with else if (Python elif, Craykon else if), you never need to ask the lower end. If you write only ifs without else, every following if is also true for a 500 g box, so its fee is overwritten to 8,500 won. If you ask with <, a box of exactly 1,000 g goes one band up. Third, the express rule needs both conditions to be true: Python not far and w <= 15000, the other seven languages !far && w <= 15000. Writing || gives express delivery to light boxes in remote regions. In Java, comparing "X" with == asks whether it is the same object, not the same text, so use equals (== in C#, Python, JS and Craykon compares the text).
Pythonmain.py · 40 lines
def base_fee(w): # 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다 if w <= 1000: return 3500 elif w <= 3000: return 4000 elif w <= 7000: return 5000 elif w <= 15000: return 6500 elif w <= 25000: return 8500 else: return None # 25,000g을 넘으면 받지 않는다input() # kremote = set(map(int, input().split())) # 수로 바꾼 집합: "12"가 "112" 안에 있다고 착각하지 않는다n = int(input())accepted = 0total = 0for _ in range(n): r, w, s = input().split() r, w = int(r), int(w) fee = base_fee(w) far = r in remote if fee is not None and far: fee += 3000 if fee is not None and s == "X": if not far and w <= 15000: # 빠른 배송은 두 조건을 모두 지켜야 한다 fee += 2000 else: fee = None if fee is None: print("reject") else: print(fee) accepted += 1 total += feeprint(accepted, total)
JavaScriptmain.js · 36 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");// 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다const baseFee = (w) => { if (w <= 1000) return 3500; else if (w <= 3000) return 4000; else if (w <= 7000) return 5000; else if (w <= 15000) return 6500; else if (w <= 25000) return 8500; else return null; // 25,000g을 넘으면 받지 않는다};// 수로 바꾼 Set: 줄 문자열에 includes를 쓰면 "12"가 "112" 안에서 찾아진다const remote = new Set(lines[1].trim().split(/\s+/).filter((x) => x !== "").map(Number));const n = Number(lines[2]);const out = [];let accepted = 0;let total = 0;for (let i = 0; i < n; i++) { const [rText, wText, s] = lines[3 + i].trim().split(/\s+/); const r = Number(rText); const w = Number(wText); let fee = baseFee(w); const far = remote.has(r); if (fee !== null && far) fee += 3000; if (fee !== null && s === "X") { if (!far && w <= 15000) fee += 2000; // 빠른 배송은 두 조건을 모두 지켜야 한다 else fee = null; } if (fee === null) out.push("reject"); else { out.push(String(fee)); accepted++; total += fee; }}out.push(`${accepted} ${total}`);console.log(out.join("\n"));
TypeScriptmain.ts · 37 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");// 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다const baseFee = (w: number): number | null => { if (w <= 1000) return 3500; else if (w <= 3000) return 4000; else if (w <= 7000) return 5000; else if (w <= 15000) return 6500; else if (w <= 25000) return 8500; else return null; // 25,000g을 넘으면 받지 않는다};// 수로 바꾼 Set: 줄 문자열에 includes를 쓰면 "12"가 "112" 안에서 찾아진다const remote = new Set(lines[1].trim().split(/\s+/).filter((x) => x !== "").map(Number));const n = Number(lines[2]);const out: string[] = [];let accepted = 0;let total = 0;for (let i = 0; i < n; i++) { const [rText, wText, s] = lines[3 + i].trim().split(/\s+/); const r = Number(rText); const w = Number(wText); let fee: number | null = baseFee(w); const far = remote.has(r); if (fee !== null && far) fee += 3000; if (fee !== null && s === "X") { if (!far && w <= 15000) fee += 2000; // 빠른 배송은 두 조건을 모두 지켜야 한다 else fee = null; } if (fee === null) out.push("reject"); else { out.push(String(fee)); accepted++; total += fee; }}out.push(`${accepted} ${total}`);console.log(out.join("\n"));
JavaMain.java · 51 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.HashSet;import java.util.Set;public class Main { static final int REJECT = -1; // 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다 static int baseFee(int w) { if (w <= 1000) return 3500; else if (w <= 3000) return 4000; else if (w <= 7000) return 5000; else if (w <= 15000) return 6500; else if (w <= 25000) return 8500; else return REJECT; // 25,000g을 넘으면 받지 않는다 } public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in.readLine(); // k Set<Integer> remote = new HashSet<>(); for (String c : in.readLine().trim().split("\\s+")) { if (!c.isEmpty()) remote.add(Integer.parseInt(c)); } int n = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); int accepted = 0; long total = 0; for (int i = 0; i < n; i++) { String[] p = in.readLine().trim().split("\\s+"); int r = Integer.parseInt(p[0]), w = Integer.parseInt(p[1]); int fee = baseFee(w); boolean far = remote.contains(r); if (fee != REJECT && far) fee += 3000; if (fee != REJECT && p[2].equals("X")) { // 문자열은 ==가 아니라 equals로 비교한다 if (!far && w <= 15000) fee += 2000; // 빠른 배송은 두 조건을 모두 지켜야 한다 else fee = REJECT; } if (fee == REJECT) { out.append("reject\n"); } else { out.append(fee).append('\n'); accepted++; total += fee; } } out.append(accepted).append(' ').append(total).append('\n'); System.out.print(out); }}
C#Main.cs · 53 lines
using System;using System.Collections.Generic;using System.Linq;using System.Text;class Program{ // 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다 static int? BaseFee(int w) { if (w <= 1000) return 3500; else if (w <= 3000) return 4000; else if (w <= 7000) return 5000; else if (w <= 15000) return 6500; else if (w <= 25000) return 8500; else return null; // 25,000g을 넘으면 받지 않는다 } static void Main() { Console.ReadLine(); // k var remote = new HashSet<int>(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)); int n = int.Parse(Console.ReadLine()); var sb = new StringBuilder(); int accepted = 0; long total = 0; for (int i = 0; i < n; i++) { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int r = int.Parse(p[0]), w = int.Parse(p[1]); int? fee = BaseFee(w); bool far = remote.Contains(r); if (fee != null && far) fee += 3000; if (fee != null && p[2] == "X") { if (!far && w <= 15000) fee += 2000; // 빠른 배송은 두 조건을 모두 지켜야 한다 else fee = null; } if (fee == null) { sb.Append("reject\n"); } else { sb.Append(fee.Value).Append('\n'); accepted++; total += fee.Value; } } sb.Append(accepted).Append(' ').Append(total).Append('\n'); Console.Write(sb); }}
C++main.cpp · 48 lines
#include <iostream>#include <optional>#include <string>#include <unordered_set>// 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다std::optional<int> baseFee(int w) { if (w <= 1000) return 3500; else if (w <= 3000) return 4000; else if (w <= 7000) return 5000; else if (w <= 15000) return 6500; else if (w <= 25000) return 8500; else return std::nullopt; // 25,000g을 넘으면 받지 않는다}int main() { int k, n; std::cin >> k; std::unordered_set<int> remote; for (int i = 0; i < k; i++) { int code; std::cin >> code; remote.insert(code); } std::cin >> n; int accepted = 0; long long total = 0; for (int i = 0; i < n; i++) { int r, w; std::string s; std::cin >> r >> w >> s; std::optional<int> fee = baseFee(w); bool far = remote.count(r) > 0; // C++20부터는 remote.contains(r) if (fee && far) *fee += 3000; if (fee && s == "X") { if (!far && w <= 15000) *fee += 2000; // 빠른 배송은 두 조건을 모두 지켜야 한다 else fee.reset(); } if (!fee) { std::cout << "reject\n"; } else { std::cout << *fee << '\n'; accepted++; total += *fee; } } std::cout << accepted << ' ' << total << '\n';}
Cmain.c · 50 lines
#include <stdio.h>#include <string.h>#define REJECT -1/* 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다 */int base_fee(int w) { if (w <= 1000) return 3500; else if (w <= 3000) return 4000; else if (w <= 7000) return 5000; else if (w <= 15000) return 6500; else if (w <= 25000) return 8500; else return REJECT; /* 25,000g을 넘으면 받지 않는다 */}int main(void) { /* 지역 번호는 1~999라서 번호를 칸 번호로 쓰는 표가 곧 "들어 있나" 검사다 */ static int is_remote[1000]; int k, n; scanf("%d", &k); for (int i = 0; i < k; i++) { int code; scanf("%d", &code); is_remote[code] = 1; } scanf("%d", &n); int accepted = 0; long total = 0; for (int i = 0; i < n; i++) { int r, w; char s[4]; scanf("%d %d %3s", &r, &w, s); int fee = base_fee(w); int far = is_remote[r]; if (fee != REJECT && far) fee += 3000; if (fee != REJECT && strcmp(s, "X") == 0) { if (!far && w <= 15000) fee += 2000; /* 빠른 배송은 두 조건을 모두 지켜야 한다 */ else fee = REJECT; } if (fee == REJECT) { printf("reject\n"); } else { printf("%d\n", fee); accepted++; total += fee; } } printf("%d %ld\n", accepted, total); return 0;}
Craykonmain.cray · 65 lines
// 아래 칸부터 차례로 묻는다: 앞 조건이 거짓일 때만 다음 조건을 보니 칸의 아래 끝은 따로 묻지 않는다기본요금(w){ if (w <= 1000) { return 3500; } else if (w <= 3000) { return 4000; } else if (w <= 7000) { return 5000; } else if (w <= 15000) { return 6500; } else if (w <= 25000) { return 8500; } return -1; // 25,000g을 넘으면 받지 않는다}Console.ReadLine(); // kremote = Console.ReadNumbers(); // 수 목록: 문자열 "12 305"에서 찾으면 "12"가 "112" 안에서도 찾아진다n = Console.ReadLine().ToNumber();accepted = 0;total = 0;for (i = 0; i < n; i++){ 칸 = Console.ReadLine().Split(); r = 칸[0].ToNumber(); w = 칸[1].ToNumber(); fee = 기본요금(w); far = remote.Contains(r); if (fee != -1 && far) { fee += 3000; } if (fee != -1 && 칸[2] == "X") { if (!far && w <= 15000) { fee += 2000; } else { fee = -1; } } if (fee == -1) { Console.WriteLine("reject"); } else { Console.WriteLine(fee); accepted += 1; total += fee; }}Console.WriteLine(accepted + " " + total);