logo AlgoBeat OnlineJudge
登录 注册

#215731. [ICPC 2024 Jakarta R] Microwavable Subsequence

内存限制:1024 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are given an array of integers: .

A subsequence can be derived from an array by removing zero or more elements without changing the order of the remaining elements. For example, , , , and are subsequences of array , while is not a subsequence of array .

A subsequence is microwavable if the subsequence consists of at most two distinct values and each element differs from its adjacent elements. For example, , , and are microwavable, while and are not microwavable.

Denote a function as the length of the longest microwavable subsequence of array such that each element within the subsequence is either or . Find the sum of for all .

输入格式

The first line consists of two integers ( ).

The second line consists of integers ( ).

输出格式

Output a single integer representing the sum of for all .

样例

样例输入 1

5 4
3 2 1 3 2

样例输出 1

13

样例输入 2

3 3
1 1 1

样例输出 2

2

数据范围与提示

Explanation for the sample input/output #1

The value of is , taken from the subsequence that can be obtained by removing and . The value of is , taken from the subsequence that can be obtained by removing and . The value of is , taken from the subsequence that can be obtained by removing . The value of , , and are all .

Explanation for the sample input/output #2

The value of and are both , while the value of is .