forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10003.cpp
53 lines (47 loc) · 934 Bytes
/
10003.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++)
#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++)
static int n, j, i, l, k, length, num_of_cut, cutPos[55], table[55][55];
void readData()
{
scanf("%d", &num_of_cut);
FOREQ(i, 1, num_of_cut)
scanf("%d", &cutPos[i]);
cutPos[0] = 0;
cutPos[num_of_cut + 1] = length;
}
void solve()
{
n = num_of_cut + 1;
FOREQ(i, 0, n)
table[i][i] = 0;
FOREQ(l, 2, n)
{
FOREQ(i, 1, n - l + 1)
{
j = i + l - 1;
table[i][j] = INF;
FOR(k, i, j)
{
int temp;
temp = table[i][k] + table[k + 1][j] + cutPos[j] - cutPos[i - 1];
if (temp < table[i][j])
{
table[i][j] = temp;
}
}
}
}
}
int main()
{
while (scanf("%d", &length) && length)
{
readData();
solve();
printf("The minimum cutting is %d.\n", table[1][num_of_cut + 1]);
}
return 0;
}