In an adventure game you keep your gold coins in a bank and go on an adventure for n days. On the morning of day i you take e_i coins out of the bank and spend them. If the bank holds fewer than e_i coins at that moment, the adventure fails. If c coins are left after taking them out, that night the bank adds, as interest, the quotient of c × r_i divided by 100. So the next morning the bank holds c + (the quotient of c × r_i divided by 100) coins. For each day from day 1 to day n, find the smallest number of coins the bank must hold that morning, before coins are taken out, so that nothing fails from that day through day n. The interest added on the night of day n is never used, so it does not affect the answers. The first line of input gives n. The second line of input gives e_1, e_2, …, e_n separated by single spaces. The third line of input gives r_1, r_2, …, r_n separated by single spaces. Print the n values, from day 1's value to day n's value, on one line separated by single spaces. Write the numbers as plain digits without commas.
Input
1 ≤ n ≤ 9,000, 0 ≤ e_i ≤ 1,000,000, 0 ≤ r_i ≤ 200, all integers
Output
For each day from day 1 to day n, the smallest number of coins needed that morning, n numbers on one line separated by single spaces
Example 1 · stdin
3
50 30 110
10 20 50
stdout
161 122 110
Example 2 · stdin
4
0 7 0 0
100 0 100 100
stdout
4 7 0 0
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
This problem builds the answer backwards from the end. You don't know the first day's coins, so you can't follow the days forward, but the morning after day n needs 0 coins. From there, go back one day at a time: find the smallest c that must remain tonight so that the next morning has a coins, then add that day's e_i. After the night the coins are c + ⌊c·r/100⌋ = ⌊c·(100+r)/100⌋, so the smallest c is a·100/(100+r) rounded up, which in integers is the quotient of (a·100 + 100 + r − 1) divided by (100 + r). The backward loop is Python range(n - 1, -1, -1), JS/TS/Java/C#/C/C++ for (i = n - 1; i >= 0; i--), and Craykon for (i = n - 1; i >= 0; i = i - 1). The quotient is Python //, integer / in Java/C#/C/C++ (everything is 0 or more, so it equals rounding down), JS/TS Math.floor(x / y), and Craykon Mathf.Quot. An answer is at most 9,000 × 1,000,000 = 9 billion and a·100 reaches about 900 billion, so even JS/TS/Craykon numbers, exact up to 2^53, stay exact. Trap 1: collecting answers in the order you compute them prints the last day first. Example 1 becomes 110 122 161 instead of 161 122 110. Store each answer in slot i and print from the front (Python " ".join, JS/TS join(" "), Java StringJoiner, C# string.Join, Craykon Join(" "); C/C++ put a space before every number from the second on). Trap 2: it is tempting to think 'take the a coins needed tomorrow and subtract the interest on a'. The interest is paid on the smaller c, not on a. On day 2 of example 1, leaving 110 − 22 = 88 coins grows to only 88 + 17 = 105 overnight, and the output becomes 157 118 110. Trap 3: using the quotient of a·100/(100+r) without rounding up can leave you one coin short. On day 2 of example 1, 11000/120 = 91.6…, and leaving 91 coins grows to 91 + 18 = 109, giving 160 121 110. Day 1 of example 2 also turns 3.5 into 3 and prints 3 7 0 0. When it divides evenly (11000/110 = 100) rounding up and the quotient agree, which makes the bug easy to miss. Trap 4: getting the order wrong, 'interest is added and then you spend', means undoing interest on (e_i + a), and example 1 becomes 125 87 74. Here you spend in the morning and interest comes at night. Trap 5: counting with int in Java/C#/C/C++ overflows. Even before an answer passes about 2.1 billion, a·100 exceeds int once a passes about 21.47 million. Java int wraps around without an exception and gives a wrong number, and signed overflow in C/C++ is undefined behaviour. Use long in Java/C# and long long in C/C++.
Pythonmain.py · 13 lines
n = int(input())spend = list(map(int, input().split()))rate = list(map(int, input().split()))# 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다need = [0] * nafter = 0 # 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0)for i in range(n - 1, -1, -1): # 밤에 c가 c + c*r//100 = (c*(100+r))//100 이 된다 # 이것이 after 이상이 되는 가장 작은 c는 after*100/(100+r)의 올림 keep = (after * 100 + 100 + rate[i] - 1) // (100 + rate[i]) need[i] = spend[i] + keep after = need[i]print(" ".join(map(str, need)))
JavaScriptmain.js · 15 lines
const data = require("fs").readFileSync(0, "utf8").split(/\s+/).filter(Boolean).map(Number);const n = data[0];const spend = data.slice(1, 1 + n);const rate = data.slice(1 + n, 1 + 2 * n);// 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다const need = new Array(n).fill(0);let after = 0; // 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0)for (let i = n - 1; i >= 0; i--) { // 밤에 c가 c + ⌊c×r/100⌋ 이 된다. 이것이 after 이상이 되는 가장 작은 c는 after×100/(100+r)의 올림 // 답은 2^53보다 훨씬 작아서 number로 정확하다 const keep = Math.floor((after * 100 + 100 + rate[i] - 1) / (100 + rate[i])); need[i] = spend[i] + keep; after = need[i];}console.log(need.join(" "));
TypeScriptmain.ts · 16 lines
import { readFileSync } from "node:fs";const data: number[] = readFileSync(0, "utf8").split(/\s+/).filter(Boolean).map(Number);const n = data[0];const spend = data.slice(1, 1 + n);const rate = data.slice(1 + n, 1 + 2 * n);// 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다const need: number[] = new Array<number>(n).fill(0);let after = 0; // 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0)for (let i = n - 1; i >= 0; i--) { // 밤에 c가 c + ⌊c×r/100⌋ 이 된다. 이것이 after 이상이 되는 가장 작은 c는 after×100/(100+r)의 올림 // 답은 2^53보다 훨씬 작아서 number로 정확하다 const keep = Math.floor((after * 100 + 100 + rate[i] - 1) / (100 + rate[i])); need[i] = spend[i] + keep; after = need[i];}console.log(need.join(" "));
JavaMain.java · 30 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringJoiner;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); long[] spend = new long[n]; long[] rate = new long[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) spend[i] = Long.parseLong(st.nextToken()); st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) rate[i] = Long.parseLong(st.nextToken()); // 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다 // 답이 90억까지 가서 int(약 21억)로는 넘친다. long을 쓴다 long[] need = new long[n]; long after = 0; // 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0) for (int i = n - 1; i >= 0; i--) { // 밤에 c가 c + c*r/100 이 된다. 이것이 after 이상이 되는 가장 작은 c는 after*100/(100+r)의 올림 long keep = (after * 100 + 100 + rate[i] - 1) / (100 + rate[i]); need[i] = spend[i] + keep; after = need[i]; } StringJoiner out = new StringJoiner(" "); for (long x : need) out.add(Long.toString(x)); System.out.println(out); }}
C#Main.cs · 24 lines
using System;using System.Linq;class Program{ static void Main() { int n = int.Parse(Console.ReadLine().Trim()); long[] spend = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray(); long[] rate = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray(); // 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다 // 답이 90억까지 가서 int(약 21억)로는 넘친다. long을 쓴다 long[] need = new long[n]; long after = 0; // 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0) for (int i = n - 1; i >= 0; i--) { // 밤에 c가 c + c*r/100 이 된다. 이것이 after 이상이 되는 가장 작은 c는 after*100/(100+r)의 올림 long keep = (after * 100 + 100 + rate[i] - 1) / (100 + rate[i]); need[i] = spend[i] + keep; after = need[i]; } Console.WriteLine(string.Join(" ", need)); }}
C++main.cpp · 22 lines
#include <iostream>#include <vector>int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<long long> spend(n), rate(n), need(n); for (auto& x : spend) std::cin >> x; for (auto& x : rate) std::cin >> x; // 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다 // 답이 90억까지 가서 int(약 21억)로는 넘친다. long long을 쓴다 long long after = 0; // 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0) for (int i = n - 1; i >= 0; i--) { // 밤에 c가 c + c*r/100 이 된다. 이것이 after 이상이 되는 가장 작은 c는 after*100/(100+r)의 올림 long long keep = (after * 100 + 100 + rate[i] - 1) / (100 + rate[i]); need[i] = spend[i] + keep; after = need[i]; } for (int i = 0; i < n; i++) std::cout << (i ? " " : "") << need[i]; std::cout << '\n';}
Cmain.c · 24 lines
#include <stdio.h>#define MAXN 9000static long long spend[MAXN], rate[MAXN], need[MAXN];int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &spend[i]); for (int i = 0; i < n; i++) scanf("%lld", &rate[i]); /* 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다 */ /* 답이 90억까지 가서 int(약 21억)로는 넘친다. long long을 쓴다 */ long long after = 0; /* 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0) */ for (int i = n - 1; i >= 0; i--) { /* 밤에 c가 c + c*r/100 이 된다. 이것이 after 이상이 되는 가장 작은 c는 after*100/(100+r)의 올림 */ long long keep = (after * 100 + 100 + rate[i] - 1) / (100 + rate[i]); need[i] = spend[i] + keep; after = need[i]; } for (int i = 0; i < n; i++) printf(i ? " %lld" : "%lld", need[i]); printf("\n"); return 0;}
Craykonmain.cray · 17 lines
n = Console.ReadLine().Trim().ToNumber();spendText = Console.ReadLine().Trim().Split(" ");rateText = Console.ReadLine().Trim().Split(" ");// 앞에서부터는 처음 금화를 모르니 정할 수 없다. 마지막 날부터 거꾸로 쌓아 간다need = NewList(n, 0);after = 0; // 다음 날 아침에 있어야 하는 금화(마지막 날 다음은 0)for (i = n - 1; i >= 0; i = i - 1){ spend = spendText[i].ToNumber(); rate = rateText[i].ToNumber(); // 밤에 c가 c + (c×r을 100으로 나눈 몫)이 된다 // 이것이 after 이상이 되는 가장 작은 c는 after×100/(100+r)의 올림 keep = Mathf.Quot(after * 100 + 100 + rate - 1, 100 + rate); need[i] = spend + keep; after = need[i];}Console.WriteLine(need.Join(" "));