Microwavable Subsequence
Time limit1sMemory limit2048 MB
Given an array, find the longest subsequence using only x and y with no equal adjacent elements, and sum these lengths over all pairs x < y up to M.
- Level
Medium7 of 10
- Topics
- Array, Segment tree, Hash map
- Solved
- No attempts yet
Problem
You are given an array of integers .
A subsequence is obtained from an array by removing zero or more elements without changing the order of the remaining elements. For example, , , , and are subsequences of , but is not.
A subsequence is microwavable if it contains at most two distinct values and each element differs from its adjacent elements. For example, , , and are microwavable, while and are not.
Let be the length of the longest microwavable subsequence of in which every element is either or . Find the sum of over all .
Input
The first line contains two integers and ().
The second line contains integers ().
Output
Print a single integer: the sum of over all .