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

#SRD0000084. Nghiên cứu lịch sử (Historical Research)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nghiên cứu lịch sử (Historical Research)

Historical Research

Source: JOI

Version: Phuoc Hung OJ Extended

Problem Statement

A diary records nn days, with a positive integer event type xix_i on day ii. For an interval of consecutive days [l,r][l,r], the importance of type vv equals vv multiplied by its frequency in that interval. For every query interval, find the maximum importance over all event types. Queries do not modify the diary.

Input

The first line contains n,qn,q, the next line contains the nn positive event types xix_i, followed by qq inclusive 1-based intervals l,rl,r.

Output

For each query, output a 64-bit integer: the maximum importance, in input order.

Subtasks

  • Subtask 1 (5 points): 1≤n≤1001\le n\le 100; 1≤q≤1001\le q\le 100. 1≤xi≤1091\le x_i\le 10^9; 1≤l≤r≤n1\le l\le r\le n.

  • Subtask 2 (10 points): 1≤n≤50001\le n\le 5000; 1≤q≤50001\le q\le 5000. 1≤xi≤1091\le x_i\le 10^9; 1≤l≤r≤n1\le l\le r\le n.

  • Subtask 3 (25 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000. 1≤xi≤1091\le x_i\le 10^9; 1≤l≤r≤n1\le l\le r\le n. Any two distinct query intervals must not be nested, i.e. there are no distinct i,ji,j with li≤lj≤rj≤ril_i\le l_j\le r_j\le r_i.

  • Subtask 4 (60 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000. 1≤xi≤1091\le x_i\le 10^9; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

5 5
9 8 7 8 9
1 2
3 4
4 4
1 4
2 4

Output

9
8
8
16
16

Explanation

In [1,2][1,2], 9 and 8 each occur once, so the answer is 9. In [1,4][1,4], type 8 occurs twice and contributes 8⋅2=168\cdot2=16, the maximum. Interval [2,4][2,4] also contains two copies of 8, yielding 16.