Cut the Cake

2.3

3 votes
Problem

On the occasion of your birthday, your friends have organized a surprise birthday party for you. In that party, there is a game planned by them. Rules of the game are as follows:-

  1. There is a cake, in the shape of a rectangle, of length m and width n.
  2. Each cut on this cake has a given cost, cost_x[i] and cost_y[i] for each cut along a row and a column respectively. This means that the cost of a cut must be multiplied by the number of segments it crosses.
  3. You have to cut the cake into 1×1 slices such that the cost of cutting is minimum.
  4. The cost of cutting the whole cake down into 1×1 slices is the sum of the costs of each successive cut.

Can you find this minimum cost? The number may be large, so print the value modulo 109+7.

Input Format:

The first line contains an integer q, the number of input queries.

The following q  sets of lines are as follows:

  • The first line has two positive space-separated integers m and n, the number of rows and columns on the cake.

  • The second line contains  m1  space-separated integers cost_y[i], the cost of a horizontal cut between rows i, and i+1  of the cake.

  • The third line contains n1 space-separated integers cost_x[j], the cost of a vertical cut between columns  jand j+1  of the cake.

Output Format:

For each of the q queries, find the minimum cost of cutting the cake into 1×1 squares and print the value of this cost module 109+7

Constraints:

1q200

2m,n100000

0cost_x[j],cost_y[i]109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Our sequence of cuts is: y[5],x[1],y[3],y[1],x[3],y[2],y[4],x[2]

  • Cut 1: Horizontal with cost cost_y[5]=4 across 1 segment. total cost=4×1=4
  • Cut 2: Vertical with cost cost_x[1]=4 across 2 segments. total cost=4×2=8
  • Cut 3: Horizontal with cost cost_y[3]across 2 segments. total cost=3×2=6 .
  • Cut 4: Horizontal with cost cost_y[1] across 2 segments.total cost=2×2=4 .
  • Cut 5: Vertical with cost cost_x[3]across 4 segments. total cost=2×4=8 .
  • Cut 6: Horizontal with cost_y[2]cost across 3 segments. total cost=1×3=3 .
  • Cut 7: Horizontal with cost_y[4] cost across 3 segments. total cost=1×3=3 .
  • Cut 8: Vertical with cost_x[2]cost across 6 segments. total cost=1×6=6

Total cost=4+8+6+4+8+3+3+6=42. We then print the value of 42 % (109+7)

Editor Image

?