2014
03-02

# Lawrence of Arabia

T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary targets were the railroads. A highly fictionalized version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia".

You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear—there are no branches, no spurs. Next, British Intelligence has assigned a Strategic Importance to each depot—an integer from 1 to 5. A depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:

Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.
Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves—they are too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked this rail line right in the middle:

The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:

The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence’s best option.

Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad.

There will be several data sets. Each data set will begin with a line with two integers, n and m. n is the number of depots on the railroad (1≤n≤1000), and m is the number of attacks Lawrence has resources for (0≤m<n). On the next line will be n integers, each from 1 to 5, indicating the Strategic Value of each depot in order. End of input will be marked by a line with n=0 and m=0, which should not be processed.

There will be several data sets. Each data set will begin with a line with two integers, n and m. n is the number of depots on the railroad (1≤n≤1000), and m is the number of attacks Lawrence has resources for (0≤m<n). On the next line will be n integers, each from 1 to 5, indicating the Strategic Value of each depot in order. End of input will be marked by a line with n=0 and m=0, which should not be processed.

4 1
4 5 1 2
4 2
4 5 1 2
0 0

17
2

/*
SER 2008.  Problem C: Lawrence of Arabia
Author: Ryan Stansifer
*/

#include <cstdio>

#define NDEBUG
#include <cassert>

int main (void) {
int ds, i, j, k, x;
int n, m;

input: for (ds=1; /**/; ds++) {
scanf ("%d", &n);
if (n==0) break;
assert (1<=n && n<=500);
scanf ("%d", &m);
assert (0<=m && m<n);

int depot[n]; // strategic value associated with each depot
for (i=0; i<n; i++) scanf ("%d", &depot[i]);

/*
We want to pre-compute the stragetic value of the portion of the
railroad from depot i to depot j.
*/

/*
First compute all partial sums for 0<=i<=n:
partial[i] = sum {depot[k] | k<-[0..i-1]}
So partial[j]-partial[i] = sum{depot[k] | k<-[i..j-1]}
And sum {depot[k] | k<-[i..j]} = partial[j+1]-partial[i]
*/
int partial[n+1];
partial[0]=0;
for (i=0; i<n; i++) {
partial[i+1]=partial[i]+depot[i];
}

/*
Now compute the stragetic value of railroad from depot i to depot j.
*/
int value[n][n];
for (i=0; i<n; i++) {
value[i][i] = 0;
for (j=i+1; j<n; j++) value[i][j] = value[i][j-1] + depot[j]*(partial[j]-partial[i]);
}

/*
Compute remain_k[j], 0<=j<n, the minimum strategic value remaining after
k attacks on the portion of the railroad from depot 0 to depot j.
*/
int remain[n];

// For k=0 attacks: remain_k[j] = value[k][j] for 0<=j<n
for (j=0; j<n; j++) remain[j] = value[0][j];

for (k=1; k<=m; k++) {
/*
Compute remain_(k+1)[j] from remain_k[j[, 0<=j<n, the
minimum strategic value remaining after an additional
attack (the k+1 attack) on the portion of the railroad
from depot 0 to depot j.
*/
for (j=n-1; j>=0; j--) {
/*
See if attacking between depot x and x+1 will result
in lower remaining strategic value.
*/
for (x=k-1; x<j; x++) {
// x<j means remain[x] is the old, remain_(k-1)[x] value
// x<j means remain[j] is the new, remain_k[j] value
int s=remain[x]+value[x+1][j];
if (remain[j]>s) remain[j] = s;
}
}
}

printf ("%d\n", remain[n-1]);
}
return 0;
}