思路:
真是菜的不行。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 int dp[11][1001], A[100005], B[100005], K[1005], P[1005], n, m; 8 9 int main() 10 { 11 while (scanf("%d %d", &n, &m) != EOF) 12 { 13 for (int i = 0; i < n; i++) scanf("%d %d", &A[i], &B[i]); 14 for (int i = 0; i < m; i++) scanf("%d %d", &K[i], &P[i]); 15 memset(dp, 0x3f, sizeof dp); 16 for (int j = 0; j <= 10; j++) 17 { 18 dp[j][0] = 0; 19 for (int i = 0; i < m; i++) 20 { 21 if (P[i] > j) 22 { 23 for (int k = 1; k <= 1000; k++) 24 { 25 dp[j][k] = min(dp[j][k], K[i] * ((k + P[i] - j - 1) / (P[i] - j))); 26 } 27 } 28 } 29 } 30 31 for (int j = 0; j <= 10; j++) 32 { 33 for (int i = 1; i < m; i++) 34 { 35 for (int k = 1; k <= 1000; k++) 36 { 37 if (P[i] > j && k - (P[i] - j) >= 0) 38 { 39 dp[j][k] = min(dp[j][k], dp[j][k - (P[i] - j)] + K[i]); 40 } 41 } 42 } 43 } 44 45 long long sum = 0; 46 bool flg = true; 47 for (int i = 0; i < n; i++) 48 { 49 if (dp[B[i]][A[i]] >= INF) 50 { 51 flg = false; break; 52 } 53 sum += (long long)dp[B[i]][A[i]]; 54 } 55 if (!flg) puts("-1"); 56 else printf("%I64d ", sum); 57 } 58 return 0; 59 }