uva 6952 Cent Savings dp

时间:2021-08-28 06:00:42

Cent Savings

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=116998

Description

To host a regional contest like NWERC a lot of preparation is necessary: organizing rooms and computers, making a good problem set, inviting contestants, designing T-shirts, booking hotel rooms and so on. I am responsible for going shopping in the supermarket.

When I get to the cash register, I put all my n items on the conveyor belt and wait until all the other customers in the queue in front of me are served. While waiting, I realize that this supermarket recently started to round the total price of a purchase to the nearest multiple of 10 cents (with 5 cents being rounded upwards). For example, 94 cents are rounded to 90 cents, while 95 are rounded to 100.

It is possible to divide my purchase into groups and to pay for the parts separately. I managed to find d dividers to divide my purchase in up to d+1 groups. I wonder where to place the dividers to minimize the total cost of my purchase. As I am running out of time, I do not want to rearrange items on the belt.

Input

The input consists of:

one line with two integers n (1≤n≤2000) and d (1≤d≤20), the number of items and the number of available dividers;
    one line with n integers p1,… pn (1≤pi≤10000 for 1≤i≤n), the prices of the items in cents. The prices are given in the same order as the items appear on the belt.

Output

Output the minimum amount of money needed to buy all the items, using up to d dividers.

Sample Input

5 1
13 21 55 60 42

Sample Output

190

HINT

题意

这个商店交款方式是4舍5入的,然后你可以把你的商品分成d堆交钱

然后问你最少交多少钱

题解:

dp题呀

就相当于有n-1个空,插d个板子

那么我们就可以DP解决他,dp[i][j]表示前i个空插了j个板子后的最小值

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int a[];
ll dp[][];
ll cal(ll x)
{
if(x%>=)
x+=;
x-=x%;
return x;
}
int main()
{
int n,d;
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
a[i]=read();
for(int i=;i<=n-;i++)
{
for(int j=;j<=d;j++)
{
if(j==)
dp[i][j]+=dp[i-][j]+a[i];
else
dp[i][j]=min(dp[i-][j]+a[i],cal(dp[i-][j-]+a[i]));
}
}
ll ans=inf;
for(int j=;j<=d;j++)
ans=min(cal(dp[n-][j]+a[n]),ans);
cout<<ans<<endl;
}