PivotOJ

Badge Relay

시간 제한: 9000ms메모리 제한: 2048MB출처: ICPC Asia Seoul Regional 2025BOJ 34861

문제

A research facility operates two buildings, Left Lab and Right Lab, connected by a single secure corridor. At the beginning, all selected employees are standing in the Left Lab.

  • The corridor can hold at most two people at a time.
  • There is exactly one security badge, and every traversal of the corridor (whether by one person or two together) must be accompanied by the badge.
  • If two people travel together, they must walk side by side, and the traversal time is equal to the slower person’s time.
  • If there are still employees remaining in the Left Lab after a traversal to the Right Lab, then someone in Right Lab must bring the badge back to the Left Lab before the next traversal from the Left Lab to the Right Lab can begin.

To see how the strategy affects the total time, consider four employees whose individual traversal times {1,2,5,10}\{1, 2, 5, 10\}. One possible strategy is as follows. If (1,2)(1, 2) traverse first (taking 22 minutes), then (1)(1) returns with the badge (11 minute), then (1,5)(1, 5) traverse (55 minutes), (1)(1) returns (11 minute), and finally (1,10)(1, 10) traverse (1010 minutes), the total time is 1919 minutes. However, there is another (better) strategy; if (1,2)(1, 2) traverse first (22 minutes), (1)(1) returns (11 minute), then (5,10)(5, 10) traverse together (1010 minutes), (2)(2) returns (22 minutes), and finally (1,2)(1, 2) traverse again (22 minutes), the total time becomes 1717 minutes, which yields a smaller total traversal time than the first. For convenience, the two crossing sequences are summarized in Tables 1 and 2 below.

Table 1. Sequence A (Total 1919 min)

Step Action Left Lab (after) Right Lab (after) Duration (min) Elapsed (min)
00 - {1,2,5,10}\{1,2,5,10\} ∅ - -
11 Cross (1,2)(1, 2) {5,10}\{5,10\} {1,2}\{1,2\} 22 22
22 Return (1)(1) {1,5,10}\{1,5,10\} {2}\{2\} 11 33
33 Cross (1,5)(1, 5) {10}\{10\} {1,2,5}\{1,2,5\} 55 88
44 Return (1)(1) {1,10}\{1,10\} {2,5}\{2,5\} 11 99
55 Cross (1,10)(1, 10) ∅ {1,2,5,10}\{1,2,5,10\} 1010 1919

Table 2. Sequence B (Total 1717 min)

Step Action Left Lab (after) Right Lab (after) Duration (min) Elapsed (min)
00 - {1,2,5,10}\{1,2,5,10\} ∅ - -
11 Cross (1,2)(1, 2) {5,10}\{5,10\} {1,2}\{1,2\} 22 22
22 Return (1)(1) {1,5,10}\{1,5,10\} {2}\{2\} 11 33
33 Cross (5,10)(5, 10) {1}\{1\} {2,5,10}\{2,5,10\} 1010 1313
44 Return (2)(2) {1,2}\{1,2\} {5,10}\{5,10\} 22 1515
55 Cross (1,2)(1,2) ∅ {1,2,5,10}\{1,2,5,10\} 22 1717

You are given nn employees. Employee ii needs TiT_i minutes to traverse the corridor. You are also given qq queries. Each query is specified by:

  • an index range [x,y][x, y] (inclusive),
  • a time range [a,b][a, b] (inclusive), and
  • a selection cap KK (the maximum number of employees you may select).

For each query, consider all employees whose indices lie in [x,y][x, y] and whose traversal times lie in [a,b][a, b]. Among these employees, select the KK employees who have the smallest individual traversal times. If fewer than KK such employees exist, select all of them. These selected employees all start in the Left Lab with the single badge. Under the rules described above, you should compute the minimum total traversal time required for all selected employees to reach the Right Lab. If no employee is selected, the answer should be zero.

Given nn employees with traversal times T1,,TnT_1, \cdots , T_n and qq queries of the form (x,y,a,b,K)(x, y, a, b, K), write a program that processes the queries and outputs, for each query, the minimum total traversal time for the selected employees to reach the Right Lab.

입력

Your program is to read from standard input. The input starts with a line containing two integers nn and qq (1 ≤ n, q ≤ 100\,000), where nn is the number of employees and qq is the number of queries. Employees are numbered from 11 to nn. The second line contains nn integers T1,,TnT_1, \cdots , T_n (1 ≤ T_i ≤ 10^9), where TiT_i is the traversal time of employee ii (1 ≤ i ≤ n). Each of the following qq lines contains five integers x,y,a,b,Kx, y, a, b, K described above, where 1 ≤ x ≤ y ≤ n; 1 ≤ a ≤ b ≤ 10^9; 1 ≤ K ≤ n.

출력

Your program is to write to standard output. For each query, print a single line containing the minimum total traversal time for the selected employees to move from the Left Lab to the Right Lab. If no employee is selected, output 00.

예제

예제 1

입력
3 3
1 2 3
1 3 1 3 3
1 3 1 3 2
1 3 4 5 1
출력
6
2
0

예제 2

입력
4 4
5 1 10 2
1 4 1 10 4
1 4 2 10 2
1 4 2 10 4
1 3 1 13 3
출력
17
5
17
16
코드를 제출하려면 로그인하세요.