POJ 3661 Running (interval dp)

Source: Internet
Author: User

Language:DefaultRunning
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5300 Accepted: 1974

Description

The cows was trying to become better athletes, so Bessie was running on a track for exactly n (1≤ n ≤10 , minutes). During each minute, she can choose to either run or rest for the whole minute.

The ultimate distance Bessie runs, though, depends on she ' exhaustion factor ', which starts at 0. When she chooses to run in minute I, she'll run exactly a distance of di (1≤ Di ≤1,000) and Her exhaustion factor would increase by 1 – but must never was allowed to exceed m (1≤ m ≤500). If she chooses to rest, her exhaustion factor would decrease by 1 for each minute she rests. She cannot commence running again until her exhaustion factor reaches 0. At the, she can choose to run or rest.

At the end of the N minute Workout, Bessie ' s exaustion factor must is exactly 0, or she'll not have enough ener Gy left for the rest of the day.

Find the maximal distance Bessie can run.

Input

* Line 1:two space-separated integers: N and M
* Lines 2. N+1:line i+1 contains the single integer: Di

Output

* Line 1: A single integer representing the largest distance Bessie can run while satisfying the conditions.
 

Sample Input

5 2534210

Sample Output

9

Source

Usaco January Silver

DP[I][J] Indicates the maximum distance the fatigue of I-moment J goes


#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace STD; #define N 10005int Dp[n][505],n,m,a[n];int main () {int i,j;while (~scanf ("%d%d", &n,&m)) {for (i=1;i<=n;i + +) scanf ("%d", &a[i]); Memset (Dp,0,sizeof (DP)); for (i=1;i<=n;i++) {for (j=1;j<=m;j++) dp[i][j]=dp[i-1][j-1 ]+a[i];            dp[i][0]=dp[i-1][0];for (int k=1;k<=m&&i>=k;k++) Dp[i][0]=max (Dp[i][0],dp[i-k][k]);} printf ("%d\n", Dp[n][0]);} return 0;}




POJ 3661 Running (interval dp)

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.