|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|2 초||128 MB||0||0||0||0.000%|
T. E. Lawrence was a controversial figure during World War I. He was a British officer, who served in the Arabian theater. He led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary targets were the railroads. A highly fictionalized version of his exploits was presented in the blockbuster movie, “Lawrence of Arabia”.
You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear – there are no branches, no spurs. Next, British Intelligence has assigned a Strategic Value to each depot – an integer from 1 to 5. But, a depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:
Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.
Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves – they’re too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked this rail line right in the middle:
The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:
The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence’s best option.
Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad.
There will be several data sets. Each data set will begin with a line with two integers, N and M. N is the number of depots on the railroad (1 <= N <= 500), and M is the number of attacks Lawrence has resources for (0 <= M < N). On the next line will be N integers, each from 1 to 5, indicating the Strategic Value of each depot in order. End of input will be marked by a line with two spaceseparated 0’s.
For each data set, print a single integer, indicating the smallest Strategic Value for the railroad that Lawrence can achieve with his attacks. Print each integer on its own line. There should be no blank lines between outputs.
4 1 4 5 1 2 4 2 4 5 1 2 0 0