12991 - Toad Jumping   

Description

There are N stones on the river. The height of the i-th stone is hi for 1 <= i <= N and is colored with color ci. Toad Epep is on the S-th stone (where the leftmost stone is the 1-st stone) and he wants to go the the T-th stone with several jumps.

For each jump from the i-th stone, Epep can jump to the (i-1)-th stone (if it exists), the (i+1)-th stone (if it exists), or any stone colored with the same color ci (if it exists).

Epep will only visit each stone at most once. That is, if Epep jumped to the k-th stone at some time, he won't jumped to the k-th stone again.

The energy cost of jump from i-th to j-th stone is |i-j| x |hi - hj|.

Because Epep ate too much insects and wants to do more exercise, can you help Epep to find the way that cost the most energy (if there are several ways with same maximum energy cost, find the way with maximum jumps) ?

Explanation of Sample IO

All possible moves are listed in the following:

  • 2 -> 1 -> 4 -> 3 -> 6 -> 5, which costs 100 energy and 5 jumps

  • 2 -> 1-> 4 -> 5, which costs 60 energy and 3 jumps

  • 2 -> 3-> 4-> 5, which costs 40 energy and 3 jumps

  • 2 -> 3 -> 6 -> 5, which costs 40 energy and 3 jumps

  • 2 -> 5, which costs 0 energy and 1 jump

Hint:

1. In this problem, you may need to use an array int jumped[N+1] to record whether a stone i has been visited in the currently expanded path.

2. However, you need to manipulate the int jumped[N+1] array properly as follows:

 

Input

Integers N, S, T are on the first line.

h1, ..., hN are on the second line.

c1, ..., cN are on the third line.

  • 1 <= N <= 15

  • 1 <= S, T <= N and S != T

  • 1 <= hi <= 100000

  • 1 <= ci <= 15

Test case description

  • Test case 1: all stones have different colors

  • Test case 2: all stones have the same color

  • The remaining test cases: no additional restrictions

Output

Output two integers E and J, where E is the maximum energy cost and J is the number of jumps in one line. Remember to add \n in the end.

Sample Input  Download

Sample Output  Download

Tags

Recursive musukashi



Discuss