#include #include #include #include #include using namespace std; #define alok(n,t) ((t*)malloc((n)*sizeof(t))) #define NLIM 61 #define MLIM 61 #define VLIM 1000111 int bank_notes[NLIM]; int notes_c = 0; void new_bank_note(int value) { int i = notes_c++; for (; i && value < bank_notes[i-1]; i--) { bank_notes[i] = bank_notes[i-1]; } bank_notes[i] = value; } int *atm_notes[MLIM]; int atm_notes_c[MLIM]; int atm_c = 0; void new_atm() { int i = atm_c++; int k; scanf("%d", &k); atm_notes_c[i] = k; atm_notes[i] = alok(k, int); for (int j = 0; j < k; j++) { int v; scanf("%d", &v); atm_notes[i][j] = v; } sort(atm_notes[i], atm_notes[i]+k); } int greedy(long long *target, int count, int *notes, int X) { int result = 0; int i = count - 1; while (X) { int note = notes[i]; if (note > X) { i--; } else { int q = X/note; X -= note*q; target[note] += q; result += q; } } return result; } int collect_change(long long *target, int X) { return greedy(target, notes_c, bank_notes, X); } int atm_receive(long long *target, int i, int X) { return greedy(target, atm_notes_c[i], atm_notes[i], X); } long long A_sergey[VLIM]; int usage[VLIM]; void _pay(long long X) { // let's be greedy too for (int i = notes_c - 1; i >= 0; i--) { int note = bank_notes[i]; usage[note] = min(A_sergey[note], X / note); X -= usage[note] * note; } for (int i = 0; X > 0 && i < notes_c; i++) { int note = bank_notes[i]; if (A_sergey[note]) { int us = min(A_sergey[note] - usage[note], (X+note-1) / note); usage[note] += us; X -= us * note; } } } int score = 0; void pay(int X) { for (int i = 0; i < notes_c; i++) { usage[bank_notes[i]] = 0; } _pay(X); int amount = 0; for (int i = 0; i < notes_c; i++) { if (i) printf(" "); int note = bank_notes[i]; A_sergey[note] -= usage[note]; amount += usage[note] * note; printf("%d", usage[note]); } printf("\n"); int delta = collect_change(A_sergey, amount - X); score += delta; } long long temp[VLIM]; int _receive(int X) { int best = -1; double score = -1e111; for (int i = 0; i < atm_c; i++) { for (int j = 0; j < notes_c; j++) { temp[bank_notes[j]] = 0; } atm_receive(temp, i, X); double nscore = 0; for (int j = 0; j < notes_c; j++) { int note = bank_notes[j]; nscore += (temp[note] + A_sergey[note] + 4.0) / (A_sergey[note] + 4.0); } if (score < nscore) { score = nscore; best = i; } } return best; } void receive(int X) { int i = _receive(X); atm_receive(A_sergey, i, X); printf("%d\n", i+1); } int V[NLIM]; char type[11]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", V + i); new_bank_note(V[i]); } for (int i = 0; i < n; i++) { int A; scanf("%d", &A); A_sergey[V[i]] = A; } while (m--) { new_atm(); } int e; scanf("%d", &e); while (e--) { scanf("%s", type); if (*type == 'A') { new_atm(); } else if (*type == 'B') { int v; scanf("%d", &v); new_bank_note(v); } else { int X; scanf("%d", &X); (*type == 'P' ? pay : receive)(X); fflush(stdout); } } }