#P5653. Maximizing Weighted Cumulative Sum

    ID: 18881 Type: Default 1000ms 256MiB

Maximizing Weighted Cumulative Sum

Maximizing Weighted Cumulative Sum

YSGH has a number xx, initially x=0x=0. Over the course of nn minutes, at each minute ii (for 1in1\le i\le n) YSGH can add an integer yy to xx, where y[k,k]y \in [-k,k]. However, after each minute, the updated value xx (denoted as bib_i) must satisfy biaib_i \le a_i.

Given a sequence of nn integers w=[w1,w2,,wn]w = [w_1, w_2, \ldots, w_n], the goal is to choose the increments so as to maximize the objective [ \sum_{i=1}^{n} b_i \cdot w_i, ] where [ b_i = x_{i-1} + y_i, \quad x_0 = 0, \quad \text{and} \quad x_i \le a_i \text{ for } 1\le i\le n. ]

It can be shown that by rewriting the sum as [ \sum_{i=1}^{n} b_i \cdot w_i = \sum_{j=1}^{n} y_j \left(\sum_{i=j}^{n} w_i\right), ] the decision at minute jj should depend on the sign of [ S(j)=\sum_{i=j}^{n} w_i. ] If S(j)0S(j) \ge 0, one should choose the maximum possible increment (subject to the constraint at that minute), and if S(j)<0S(j) < 0, one should choose the minimum allowed increment.

You are to compute and output the maximum achievable value of the objective.

inputFormat

The input consists of three lines:

  • The first line contains two integers nn and kk, denoting the number of minutes and the maximum absolute increment allowed each minute, respectively.
  • The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n, where aia_i is the upper bound for xx after the iith minute.
  • The third line contains nn integers w1,w2,,wnw_1, w_2, \ldots, w_n, representing the weight sequence.

outputFormat

Output a single integer: the maximum possible value of [ \sum_{i=1}^{n} b_i \cdot w_i, ] subject to the conditions described.

sample

3 5
8 10 15
1 2 3
70