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

#BS0000168. Xóa một khoảng giá trị (Range Deleting)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xóa một khoảng giá trị (Range Deleting)

Range Deleting

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n of integers between 11 and xx. Choose two integers l,rl,r with 1≤l≤r≤x1\le l\le r\le x. Delete every array element whose value belongs to [l,r][l,r]; the remaining elements keep their original relative order.

For example, deleting the values in [2,4][2,4] from [4,1,1,4,5,2,4,3][4,1,1,4,5,2,4,3] leaves [1,1,5][1,1,5].

Count the pairs (l,r)(l,r) for which the remaining array is non-decreasing. Empty arrays and one-element arrays are non-decreasing. Different pairs are counted separately, even if they produce identical remaining arrays.

Input

  • The first line contains integers n,xn,x, the array length and the maximum permitted value.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in their original order.

Output

Print the number of valid pairs (l,r)(l,r). The answer may exceed a signed 32-bit integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤x≤301\le x\le30; 1≤ai≤x1\le a_i\le x for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le3000; 1≤x≤50001\le x\le5000; 1≤ai≤x1\le a_i\le x for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤1061\le n\le10^6; 1≤x≤1061\le x\le10^6; 1≤ai≤x1\le a_i\le x for every 1≤i≤n1\le i\le n.

Examples

Input

3 3
2 3 1

Output

4

Explanation

The original array is [2,3,1][2,3,1]. There are six possible deletion intervals:

  • [1,1][1,1] leaves [2,3][2,3]: valid.
  • [1,2][1,2] leaves [3][3]: valid.
  • [1,3][1,3] leaves an empty array: valid.
  • [2,2][2,2] leaves [3,1][3,1]: invalid because 3>13>1.
  • [2,3][2,3] leaves [1][1]: valid.
  • [3,3][3,3] leaves [2,1][2,1]: invalid because 2>12>1.

Exactly four intervals are valid, so the output is 4.