#P8277. [USACO22OPEN] Up Down Subsequence P
[USACO22OPEN] Up Down Subsequence P
Problem Description
Farmer John has cows (), numbered , arranged into a permutation of . You are also given a string of length consisting of the letters U and D. Find the largest such that there exists a subsequence of satisfying, for all , if the -th character of the string is U then , and if the -th character is D then .
Input Format
The first line contains .
The second line contains .
The last line contains the given string.
Output Format
Output the maximum possible value of .
5
1 5 3 4 2
UDUD
4
5
1 5 3 4 2
UUDD
3
Hint
【Sample Explanation 1】
We can choose ; the entire permutation matches the given string.
【Sample Explanation 2】
We can choose .
【Test Point Properties】
- Test points 3-4 satisfy .
- Test points 5-8 satisfy .
- In test points 9-12, all U in the string appear before all D.
- Test points 13-22 have no additional constraints.
Translated by ChatGPT 5