루미 알고리즘

#1585다이아Buying Fika

PO 2024 훈련 캠프코딩출제 Joshua Andersson영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Ever since little Kalle won a free car wash from a scratch-off ticket, he has started to see life differently. He has begun to notice that he has an immense amount of luck in everyday situations. A fraction of the extraordinary events he has recently experienced includes winning the city's annual laziness competition due to him oversleeping, arriving on time at school every day despite taking the last bus, and scoring 1.4 out of 2 points on the university entrance exam by randomly selecting answers.

He now intends to use his immense luck to help the parliament: he plans to solve an instance of the infamous knapsack problem. The parliament has a fika budget of C Swedish kronor, and their fika supplier has a selection of N different candy bags. Each bag i has a measure of how delicious it is, s_i, and a cost of c_i kronor. You can only buy each candy bag once. The goal is to purchase a subset of all the bags, such that the total cost does not exceed C, while maximizing the total level of deliciousness. In general, this is a very difficult problem to solve, but he hopes to solve it quickly by leveraging his luck.

His idea is as follows: first, take all the bags and arrange them in a random order. Then, ignore the first K bags. After that, he will consider each bag in order and buy it if he can afford it, otherwise skip it and move on. He continues this process until he has considered all N-K candy bags.

If he is lucky with his chosen order, some K will give the optimal solution. After a lot of effort, Kalle has changed the order of all the bags, and he now asks for your help in executing his algorithm for each K=0,1,…,N-1.

Input

The first line of input contains two integers N and C (1 ≤ N ≤ 2 · 10^5, 1 ≤ C ≤ 10^9), the number of candy bags and the parliament's fika budget in kronor.

The second line of input contains N integers s_1, s_2, …, s_N (1 ≤ s_i ≤ 10^9), the deliciousness of candy bag i.

The third line contains N integers c_1, c_2, …, c_N (1 ≤ c_i ≤ 10^9), the price of candy bag i in kronor.

Note that the order of candy bags is not chosen uniformly randomly.

Output

Output N integers separated by spaces, the total deliciousness of the subset of bags that Kalle's algorithm finds for K=0,1,…,N-1 in order.

Points

Your solution will be tested on several test case groups. To get the points for a group, it must pass all the test cases in the group.

GroupPoint valueConstraints
19N ≤ 1000
212C ≤ 50
311c_i ≤ c_{i+1} for all i=1,2,…,N-1.
417c_i is chosen uniformly randomly (See https://en.wikipedia.org/wiki/Discrete_uniform_distribution) between 1 and C.
551No additional constraints.

예제

예제 1

입력
3 15
8 6 10
10 8 6
출력
8 16 10 

예제 2

입력
2 2
1 2
1 2
출력
1 2 

출처 표기

출처: Programmeringsolympiaden 2024년 훈련 캠프 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.