PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000252. Số ngày ít nhất để làm đủ bó hoa (Minimum Number of Days to Make m Bouquets)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số ngày ít nhất để làm đủ bó hoa (Minimum Number of Days to Make m Bouquets)

Minimum Number of Days to Make m Bouquets

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn flowers in a row, numbered 1 through nn. Flower ii becomes available on day aia_i. You need mm bouquets, each containing exactly kk consecutive flowers in the original row. Each flower may be used in at most one bouquet. Find the earliest day when all bouquets can be made, or determine that this is impossible.

Input

The first line contains n,m,kn,m,k: the number of flowers, bouquets required and flowers per bouquet.

The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, representing the blooming days.

Output

Print the earliest feasible day, or -1 if impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ai≤501\le a_i\le 50 for every 1≤i≤n1\le i\le n; 1≤m≤201\le m\le 20; 1≤k≤n1\le k\le n.
  • Subtask 2 (30 points): 1≤n≤1051\le n\le 10^5; 1≤ai≤1091\le a_i\le 10^9 for every 1≤i≤n1\le i\le n; 1≤m≤1061\le m\le 10^6; 1≤k≤n1\le k\le n; k=1k=1.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le 10^5; 1≤ai≤1091\le a_i\le 10^9 for every 1≤i≤n1\le i\le n; 1≤m≤1061\le m\le 10^6; 1≤k≤n1\le k\le n.

Examples

Input

7 2 3
7 7 7 7 12 7 7

Output

12

Explanation

On day 7 the available runs are positions 1–4 and 6–7. Only the first run yields a bouquet of three. Nothing changes before day 12. On day 12, positions 1–3 and 4–6 form two disjoint bouquets, so 12 is minimal.