Sicily 1695. E-book Zealot

来源:互联网 发布:下载图片报网络错误 编辑:程序博客网 时间:2024/05/22 02:06

1695. E-book Zealot

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Association for Cost Minimum (ACM) is a non-profit organization which is engaged in helping people to save resource and money. Now, ACM wants to develop a kind of software to help so-called e-Book zealot to save money. 

The e-Book zealot enjoys reading book on-line very much; sometimes he reads more than 10 books a day, nobody knows how long he sleeps a day. The e-Books are not free of charge. In fact, they’re not cheap. From Figure1, ACM knows that how many books the zealot has read. 

Day 1

Day 2

Day 3

Day 4

Day 5

1

1

5

1

1

Figure 1 

 The zealot read only 1 book on day1 and read 5 books on day3. And from figure2, ACM knows the cost of reading one e-Book. Unfortunately, the cost varies instead of being constant.

 

Day 1

Day 3

Day 5

5

1

2

Figure 2

 The cost of Day1 and Day2 is 5 per e-Book, and it changed to 1 per e-Book on Day3, and it changed to 2 per e-Book on Day5. Therefore, the zealot should pay  to the e-Book provider for the 9 books he read. 

 

In order to attract more people to read on-line, the e-Book provider promotes some set-menus. See the figure 3 below. 

Set Menu

A1

A2

Number of Continued Books

2

4

Cost

6

7

Figure 3

 It means if the zealot chooses Set Menu A1, he will pay 6 for reading 2 consecutive books; and if he chooses Set Menu A2, he will pay 7 for reading 4 consecutive books. For example, if he pays the first 2 books by Set Menu A1, and pays the next 4 books by Set Menu A2, then pays the last 3 books without any set menus, he’ll pay 6 + 7 + 1 + 1 + 2 = 17. In fact, if he pays the first 4 books by Set Menu A2, and pays last 5 without any set menus, he’ll pay . Of course, the Set Menu can be used any times if necessary. And if necessary, the w-books Set Menu can use to pay q (0 < w) book(s); for example, the zealot can pay 7 for 3 continued books by Set Menu A2 (3 < 4).

 

Further more, the e-Book provider promotes another kind of set-menus. In these set-menus, the provider doesn’t care about the number of the books the zealot reads; he cares about how many days the zealot reads. See figure4. 

Set Menu

B1

B2

Number of ContinuedReading Days

3

4

Cost

9

12

Figure 4

 

It means that if the zealot chooses the Set Menu B1, he will pay 9 for reading any number books within 3 consecutive days; and if he chooses Set Menu B2, he will pay 12 for reading books of 4 continued days. For example, if he pays first 4 days reading by Set Menu B2, and pays last day reading without any set menus, he’ll pay . Similarly, the set menus can be used any times. And the w-days Set Menu can use to pay q (0 < w) day(s) reading. 

 

Now, your task is to write a program to help the zealot. Help him to minimize the money he should pay according to the number of books he reads, the prices and the set menus. 

Input

The input consists of several test cases. The first line of each test case contains only one integer n (0 < ≤ 1,000), representing the number of days the zealot has read. The second line contains n non-negative integers d1d2… dn ( ∑di<=10,000), representing the number of books the zealot read on Day 1, Day 2, …, Day n. The third line contains only one integer n1 (0 < n≤ 1,000), representing the number of different price. Then n1 integer pairs follow in separated lines. 

c1 p1

c2 p2

       …

      cnpn1

(c= 1, cc< … < cn≤ np1p2… pn1 > 0)

It means that at ci day, the price of reading one book changes to pi (0 < ≤ n1). In the next line, one integer n2 (0 ≤ n2 ≤ 1,000), representing the number of set menu A (Books Set Menus, see the figure 3). Then n2 integer pairs are in the following separated lines. 

a1 r1

a2 r2

an2 rn2

(0 < aa< … < an≤ 10,000, r1r2… rn2 > 0)

It means that the zealot can pay ri for reading ai (or less ai) consecutive books (0 < i≤ n2). Then one integer n3 (0 ≤ n≤ 1,000) follows in then next line, representing the number of set menu B (Days Set Menus, see the figure 4). Then n3 integer pairs are in the following separated lines. 

b1 s1

b2 s2

bn3 sn3

(0 < bb< … < bn≤ 1,000, s1s2… sn3 > 0)

It means that the zealot can pay si for reading books of bi (or less bi) continued days (0<in3). 

The input is terminated by a line with one zero. 

Output

For each test case, output only one single line contains the least money the zealot should pay.

Sample Input

51 1 5 1 131 53 15 222 64 723 94 120

Sample Output

12

// Problem#: 1695// Submission#: 3421689// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <iostream>#include <vector>#include <string>#include <stack>#include <iomanip>#include <algorithm>#include <queue>#include <functional>#include <map>#include <string.h>#include <math.h>#include <list>using namespace std;const int MAXD = 1005;const int MAXB = 10005;const int MAXM = 1005;int nDay;int nBook;int date[MAXB];int cost[MAXD];int iBook[MAXD];int r1;int a[MAXM], r[MAXM];int r2;int b[MAXM], s[MAXM];int solve() {    int dp[MAXB];    int i, j, k, t;    dp[0] = 0;    for (k = 1; k <= nBook; k++) {        dp[k] = dp[k - 1] + cost[date[k]];        for (i = 0; i < r1; i++) {            j = k - a[i];            if (j < 0) j = 0;            t = dp[j] + r[i];            if (t < dp[k]) dp[k] = t;            if (!j) break;        }        for (i = 0; i < r2; i++) {            j = date[k] - b[i] + 1;            if (j < 1) j = 1;            t = dp[iBook[j] - 1] + s[i];            if (t < dp[k]) dp[k] = t;            if (j == 1) break;        }    }    return dp[nBook];}int main() {    //std::ios::sync_with_stdio(false);    int n1, n2, n3;    int p, c, x, y, i, j;    while (scanf("%d", &nDay) == 1) {        if (!nDay) break;        for (nBook = 0, i = 1; i <= nDay; i++) {            scanf("%d", &x);            iBook[i] = nBook + 1;            while (x--) date[++nBook] = i;        }        scanf("%d%d%d", &n1, &p, &c);        for (i = 1; i < n1; i++) {            scanf("%d%d", &x, &y);            for (j = p; j < x; j++) cost[j] = c;            p = x;            c = y;        }        for (j = p; j <= nDay; j++) cost[j] = c;        scanf("%d", &n2);        r1 = 0;        if (n2 > 0) {            scanf("%d%d", &a[0], &r[0]);            for (i = 1; i < n2; i++) {                scanf("%d%d", &x, &y);                while (r1 >= 0 && y < r[r1]) r1--;                r1++;                a[r1] = x;                r[r1] = y;            }            r1++;        }        scanf("%d", &n3);        r2 = 0;        if (n3 > 0) {            scanf("%d%d", &b[0], &s[0]);            for (i = 1; i < n3; i++) {                scanf("%d%d", &x, &y);                while (r2 >= 0 && y < s[r2]) r2--;                r2++;                b[r2] = x;                s[r2] = y;            }            r2++;        }        printf("%d\n", solve());    }    return 0;}                                 


0 0
原创粉丝点击