1404: Computers

Time Limit: 1 Sec   Memory Limit: 128 MB

[​Submit​​][​Status​​][​Web Board​​]


Description


Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal with. You can replace your computer and get a brand new one, thus saving some maintenance cost. Of course, you must pay a fixed cost for each new computer you get.

Suppose you are considering an n year period over which you want to have a computer. Suppose you buy a new computer in year y1<=y<=n Then you have to pay a fixed cost c, in the year y, and a maintenance cost m(y,z) each year you own that computer, starting from year y through the year zz<=n, when you plan to buy - eventually - another computer.

Write a program that computes the minimum cost of having a computer over the n year period.


Input


The program input is from a text file. Each data set in the file stands for a particular set of costs. A data set starts with the cost c for getting a new computer. Follows the number n of years, and the maintenance costs m(y,z)y=1..nz=y..n. The program prints the minimum cost of having a computer throughout the n year period. All integers in the problem are bigger than 0 and no more than 1000.

White spaces can occur freely in the input. The input data are correct and terminate with an end of file.


Output


For each set of data the program prints the result to the standard output from the beginning of a line.

Sample Input

335 7 506 810


Sample Output

19


HINT


An input/output sample is shown above. There is a single data set. The cost for getting a new computer is c=3. The time period n is n=3 years, and the maintenance costs are:




  • For the first computer, which is certainly bought:m(1,1)=5,m(1,2)=7,m(1,3)=50,
  • For the second computer, in the event the current computer is replaced:m(2,2)=6,m(2,3)=8,
  • For the third computer, in the event the current computer is replaced:m(3,3)=10.



【分析】


  题目读懂就好了...简单dp


重新算了一遍方程...发现自己果然还是只会做这种简单的dp....


要记得第一年必须买电脑就行了。。因为数据比较小..(n^2)/2一维数组直接跑吧...


方程很简单  f[i]=min(f[j]+n+a[j][i])  1<=j<=i;




【代码】


#include <stdio.h>
int f[1010];
int a[1010][1010];


int min(int x,int y)
{
if (x<y) return x;else return y;
}


int main()
{
int pp;
while (~scanf("%d",&pp))
{
int n;scanf("%d",&n);
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++) scanf("%d",&a[i][j]);
f[0]=0;
for (int i=1;i<=n;i++)
{
f[i]=2100000000;
for (int j=0;j<i;j++) f[i]=min(f[i],f[j]+pp+a[j+1][i]);
}
printf("%d\n",f[n]);
}
}