Backpack DP: Turtle chess

Source: Internet
Author: User

Title Description
Description

Xiao Ming's birthday, his father gave him a pair of turtle chess as a gift. The chess board is a row of n squares, one score per lattice (non-negative integer). The 1th grid is the only starting point, nth grid is the end, the game requires the player to control a turtle piece from the starting point to go to the end.

...... 1 2 3 4 5 ... N the M-card crawl card, divided into 4 different types (m card does not necessarily contain all 4 types of cards, see sample), each type of card is marked with 1, 2, 3, 44 digits, indicating that after the use of this card, the turtle pieces will crawl forward the corresponding number of squares. In the game, each time the player needs to select a crawl card from all the crawl cards, and control the corresponding number of the turtle pieces forward, each card can only be used once. In the game, the turtle piece automatically obtains the starting point lattice the fraction, and in the subsequent crawl each arrives a lattice, obtains the corresponding score of the lattice. The player's final game score is the sum of all the squares of the turtle pieces from the beginning to the end of the process. Obviously, the use of different crawl cards in the order will make the final game score different, Xiaoming wants to find a card in order to make the final game score the most. Now, tell the score of each lattice on the board and all the crawling cards, can you tell xiaoming how many points he can get?

Enter a description Input Description

The input is separated by a space between two numbers in each row. The 1th row 2 positive integers n and M, respectively, representing the number of checkerboard squares and creeping cards. Row 2nd n non-negative integers, a1a2......an

, where AI represents the score on the first lattice of the chessboard. Line 3rd m Integer, B1B2......BM

That represents the number on the M-card crawl card. Input data guaranteed to reach the end point just ran out of M-card crawling cards, namely n-1=∑ (1->m) bi

Output description Output Description

Output one line of integers

Sample input Sample Input

13 8

4 96 10 64 55 13 94 53 5 24 89 8 30

1 1 1 1 1 2 4 1

Sample output Sample Output

455

Data range and Tips
Data Size & Hint
"Data Range"
There is 1≤n≤30,1≤m≤12 for 30% of the data.
For 50% of the data is 1≤n≤120,1≤m≤50, and 4 kinds of crawling cards, each card will not exceed the number of
Over 20.
For 100% of the data is 1≤n≤350,1≤m≤120, and 4 kinds of crawling cards, each card number of cards will not
More than 40;0≤ai≤100,1≤i≤n;1≤bi≤4,1≤i≤m. Input Data Assurance N−1=σm
I B1

Assuming the remaining T1 Zhang 1,t2 Zhang 2,t3 Zhang 3,t4 Zhang 4, the maximum score is F[T1,T2,T3,T4]
Then push forward, the last one may be taken away is 1,2,3,4, the maximum value in the inside
var a,b:array[1..350] of Longint;
F:ARRAY[0..40,0..40,0..40,0..40] of Longint;
N,i,j,k,m,t1,t2,t3,t4,i1,i2,i3,i4:longint;
Begin
READLN (N,M);
For I:=1 to N do
Read (A[i]); Each point of the weight value
For J:=1 to M do
Begin
Read (B[j]); Number of cards per type
If B[j]=1 then Inc (T1);
If B[j]=2 then Inc (T2);
If B[j]=3 then Inc (T3);
If B[j]=4 then Inc (T4);
End
F[0,0,0,0]:=A[1];
For i1:=0 to T1 do
For i2:=0 to T2 do
For i3:=0 to T3 do
For i4:=0 to T4 do
Begin
if (i1>0) and (F[i1-1,i2,i3,i4]+a[i1*1+i2*2+i3*3+i4*4+1]>f[i1,i2,i3,i4])
Then f[i1,i2,i3,i4]:=f[i1-1,i2,i3,i4]+a[i1*1+i2*2+i3*3+i4*4+1];

if (i2>0) and (F[i1,i2-1,i3,i4]+a[i1*1+i2*2+i3*3+i4*4+1]>f[i1,i2,i3,i4])
Then f[i1,i2,i3,i4]:=f[i1,i2-1,i3,i4]+a[i1*1+i2*2+i3*3+i4*4+1];

if (i3>0) and (F[i1,i2,i3-1,i4]+a[i1*1+i2*2+i3*3+i4*4+1]>f[i1,i2,i3,i4])
Then f[i1,i2,i3,i4]:=f[i1,i2,i3-1,i4]+a[i1*1+i2*2+i3*3+i4*4+1];

if (i4>0) and (F[i1,i2,i3,i4-1]+a[i1*1+i2*2+i3*3+i4*4+1]>f[i1,i2,i3,i4])
Then f[i1,i2,i3,i4]:=f[i1,i2,i3,i4-1]+a[i1*1+i2*2+i3*3+i4*4+1];
End
Writeln (F[t1,t2,t3,t4]);
End.

Backpack DP: Turtle chess

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.