#include #include #include #include #include "math.h" using namespace std; struct Book { string name; int quant; int price; Book(string n, int q, int p) : name(n), quant(q), price(p) {} }; string readTitle(istream & cin) { string result = ""; while (cin.peek() != '"' && cin.peek() != -1) cin.get(); if (cin.peek() == -1) return result; result += cin.get(); while (cin.peek() != '"' && cin.peek() != -1) result += cin.get(); if (cin.peek() != -1) result += cin.get(); return result; } int main () { int N; cin >> N; vector books; for (int i = 0; i < N; i++) { string title = readTitle(cin); int price, quant; cin >> price >> quant; Book b(title, quant, price); books.push_back(b); } cin >> N; int orders; vector temp; for (int c = 0; c < N; c++) { cin >> orders; int sum = 0; temp.clear(); for (int i = 0; i < (int)(books.size()); i++) { temp.push_back(books[i]); } for (int b = 0; b < orders; b++) { string title = readTitle(cin); if (sum == -1) continue; // just skip the rest if we can't do it int index = -1; int min = INT_MAX; for (int i = 0; i < (int)(books.size()); i++) { if (books[i].name == title && books[i].quant > 0 && books[i].price < min) { min = books[i].price; index = i; } } if (index == -1) { books.clear(); for (int i = 0; i < (int)(temp.size()); i++) { books.push_back(temp[i]); } sum = -1; } else { sum += min; books[index].quant--; } } if (sum == -1) { cout << "Cannot fill order" << endl; } else cout << sum << endl; } return 0; }