2063: [USACO 2025 January Contest, Silver] Problem 1. Cow Checkups

文件提交:无需freopen 内存限制:256 MB 时间限制:16.000 S
评测方式:普通裁判 命题人:
提交:2 解决:1

题目描述

Farmer John's  () cows are standing in a line, with cow  at the front of the line and cow  at the back of the line. FJ's cows also come in many different species. He denotes each species with an integer from  to . The 'th cow from the front of the line is of species  (). 

FJ is taking his cows to a checkup at a local bovine hospital. However, the bovine veterinarian is very picky and wants to perform a checkup on the 'th cow in the line, only if it is species  (). 

FJ is lazy and does not want to completely reorder his cows. He will perform the following operation exactly once.

  • Select two integers  and  such that . Reverse the order of the cows that are between the -th cow and the -th cow in the line, inclusive. 

FJ wants to measure how effective this approach is. Find the sum of the number of cows that are checked by the veterinarian over all  possible operations.

输入

The first line contains an integer N.

The second line contains .

The third line contains .

输出

Output one line with the sum of the number of cows that are checked by the veterinarian over all possible operations.

样例输入

3
1 3 2
3 2 1

样例输出

3

提示

If FJ chooses (), (), or () then no cows will be checked. Note that those operations do not modify the location of the cows.

The following operations result in one cow being checked.

  • : FJ reverses the order of the first and second cows so the species of each cow in the new lineup will be . The first cow will be checked.
  • : FJ reverses the order of the second and third cows so the species of each cow in the new lineup will be . The second cow will be checked.
  • : FJ reverses the order of the first, second, and third cows so the species of each cow in the new lineup will be . The third cow will be checked. 

The total number of cows checked over all six operations is .


SAMPLE INPUT:

3

1 2 3

1 2 3

SAMPLE OUTPUT:

12

There are three possible operations that cause  cows to be checked: (l), ( ), and (l). The remaining operations each result in  cow being checked. The total number of cows checked over all six operations is .


SAMPLE INPUT:

7

1 3 2 2 1 3 2

3 2 2 1 2 3 1

SAMPLE OUTPUT:

60

SCORING:

  • Input 4: 
  • Input 5: 
  • Inputs 6-9:  are all generated uniformly at random in the range 
  • Inputs 10-15:  are all generated uniformly at random in the range 
  • Inputs 16-23: No additional constraints.


来源/分类