#P8454. Optimal Segment Selection with Preference Constraints

    ID: 21630 Type: Default 1000ms 256MiB

Optimal Segment Selection with Preference Constraints

Optimal Segment Selection with Preference Constraints

Little A has n unpracticed problems. The difficulty of the i-th problem is \(x_i\). He also evaluates his own level as \(w\), which may change from query to query. For each problem, the benefit of practicing it depends on how close the problem difficulty is to his current level. Specifically, the benefit for problem i is given by:

[ \text{benefit}(i)= \begin{cases} inc, & \text{if } |x_i - w| \leq b_1,\ 0, & \text{if } b_1 < |x_i - w| \leq b_2,\ dec, & \text{if } |x_i - w| > b_2,\ \end{cases} ]

Here, it is guaranteed that \(b_1 \le b_2\) and \(dec < 0 < inc\).

Furthermore, Little A has some problems he likes and some he dislikes. He will be unhappy if in the chosen segment he either does not practice any liked problem or practices any disliked problem.

Little A will choose a contiguous segment of problems to practice such that:

  • The total benefit sum is maximized.
  • The segment contains at least one liked problem and no disliked problems.

Your task is to find and output the maximum total benefit of such a segment.

Note: Each query is independent.

inputFormat

The input consists of three lines:

  1. The first line contains six integers: n w b1 b2 inc dec, where
    • \(n\) is the number of problems,
    • \(w\) is Little A's current level,
    • \(b1\) and \(b2\) are the two thresholds with \(b1 \le b2\),
    • inc is the gain when the difficulty and level are close (\(|x_i - w| \le b1\)),
    • dec is the negative gain when they differ greatly (\(|x_i - w| > b2\)).
  2. The second line contains \(n\) integers: \(x_1, x_2, \ldots, x_n\), representing the difficulties of the problems.
  3. The third line contains \(n\) integers, each being one of 1, 0, or -1, representing the status of each problem:
    • 1 indicates a liked problem,
    • -1 indicates a disliked problem,
    • 0 indicates a neutral problem.

outputFormat

Output a single integer which is the maximum total benefit that Little A can achieve by choosing a valid contiguous segment of problems.

sample

5 10 2 4 5 -3
9 8 12 11 15
1 0 0 1 0
20