Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2986 | Accepted: 1225 |
Description
Write a program which for the given list of orders from single stations on the way from A to B determines the biggest possible total earning of the TransRuratania company. The earning from one accepted order is the product of the number of passengers included in the order and the price of their train tickets. The total earning is the sum of the earnings from all accepted orders.
Input
Output
Sample Input
10 3 4 0 2 1 1 3 5 1 2 7 2 3 10 10 5 4 3 5 10 2 4 9 0 2 5 2 5 8 0 0 0
Sample Output
19 34
#include <stdio.h>
#include <string.h>
int orders[27][4];
int maxPeoples, endStation, numOrders;;
int max;
int leane[8];
void train(int n, int sum)
{
int i;
if (sum > max)
max = sum;
if(n >= numOrders)
return;
for (i = orders[n][0]; i < orders[n][1]; i++)
if (leane[i]+orders[n][2] > maxPeoples)
break;
if (i >= orders[n][1]) {
for (i = orders[n][0]; i < orders[n][1]; i++)
leane[i] += orders[n][2];
train(n+1, sum+orders[n][3]);
for (i = orders[n][0]; i < orders[n][1]; i++)
leane[i] -= orders[n][2];
}
train(n+1, sum);
}
int main()
{
int i;
int a, b, p;
while (scanf("%d%d%d", &maxPeoples, &endStation, &numOrders), maxPeoples||endStation||numOrders) {
memset(leane, 0, sizeof(leane));
for (i = 0; i < numOrders; i++) {
scanf("%d%d%d", &a, &b, &p);
orders[i][0] = a;
orders[i][1] = b;
orders[i][2] = p;
orders[i][3] = (b-a)*p;
}
max = 0;
train(0, 0);
printf("%d\n", max);
}
return 0;
}