Tricks of the Trade

시간 제한7초메모리 제한1024 MB

요약
연속한 로봇 구간을 사서 그중 정확히 K개를 팔아 이익을 최대로 만들고, 최적 거래에 포함될 수 있는 로봇을 모두 표시한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 그리디, 세그먼트 트리, 구간
정답자
아직 제출이 없습니다

문제

Since your career as an art thief turned out to be decidedly less glamorous than you had hoped for, you have set your eyes on a new target: you want to steal a medal at this year’s CEOI!* So far your evil plan is working as intended: The Scientific Committee, still confused about the oil shortage you had arranged, immediately fell for your feigned hacking attempt. And when they let you into their secret headquarters to help plan the rejudging, you could easily steal the combination for the safe in which the medals are stored…

However, the next phase of your plan will require an assistant, and right now you don’t have enough money to hire one. Fortunately, you just found an opportunity to fix this problem. A shop you stumbled upon in Berlin sells NN programmable vacuum cleaner robots, numbered from 11 to NN. Robot ii costs c_ic\_i euros. Unfortunately, the vendor will only sell you a full, contiguous segment of robots: If you want to buy robots ii and jj, then you must buy every robot kk with i≤k≤ji ≤ k ≤ j.

As your fellow contestants are quite fond of robots, you promised to sell them KK of these robots (1≤K≤N1 ≤ K ≤ N). They will pay you s_is\_i euros for robot ii, and you hope to make a good profit in the process.

Write a program that

  • computes the maximum profit you can make by buying a segment of at least KK robots and selling exactly KK robots of this segment to the other contestants, and
  • determines which robots you might sell to the other contestants as part of such a transaction with maximum profit.†

* Obviously, the problem was with the art, not the thefts.

† After all, you might find some use for robots that you keep…

입력

The first line of input contains the numbers NN and KK as described above. The second line contains the NN integers c_1,…,c_Nc\_1 , \dots , c\_N. Finally, the third line contains the NN integers s_1,…,s_Ns\_1 , \dots , s\_N.

출력

Your program should output two lines. The first line should contain a single integer, the maximum profit you can achieve. On the second line, you should output a binary string of length NN: the ii-th character should be 11 if you might sell the ii-th robot in some transaction with maximum profit and 00 otherwise.

힌트

In the first example, you can buy the third to fifth robot and sell all three of them to your fellow contestants. This costs you 2+3+6=112 + 3 + 6 = 11 euros, while the contestants will only pay you 5+2+3=105 + 2 + 3 = 10 euros for these robots, so you lose 1 euro. If you buy any other segment of robots, you would lose even more money.

In the second example, you can buy the first to third robot and sell the first and third robot to your fellow contestants. This costs you 88 euros, while the contestants will pay you 1010 euros, so your profit is 22 euros. No other segment of robots would give you a higher profit. However, you could also buy the third and fourth robot, and sell both of them, or buy the third to fifth robot, and sell the third and fifth robot. In both cases, your profit would also be 22 euros, and so all robots except for the second robot can be part of a transaction with maximum profit.

예제2

  1. 예제 1

    입력
    5 3
    3 5 2 3 6
    2 1 5 2 3
    
    예상 출력
    -1
    00111
    
  2. 예제 2

    입력
    5 2
    1 6 1 5 2
    4 1 6 2 4
    
    예상 출력
    2
    10111