Problem:
カードの組み合わせ
整数が書いてあるカードが何枚か入っている袋を使ってゲームをしましょう。各回のゲームで参加者はまず、好きな数
n
を一つ宣言します。そして、袋の中から適当な枚数だけカードを一度に取り出して、それらのカードに書かれた数の総和が
n
に等しければ豪華賞品がもらえます。なお、それぞれのゲーム終了後カードは袋に戻されます。
袋の中の
m
種類のカードの情報および、
g
回のゲームで参加者が宣言した数を入力とし、それぞれのゲームで豪華商品をもらえるカードの組み合わせが何通りあるかを出力するプログラムを作成してください。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。
m
a
1
b
1
a
2
b
2
:
a
m
b
m
g
n
1
n
2
:
n
g
１行目にカードの種類数
m
（1 ≤
m
≤ 7）、続く
m
行に
i
種類目のカードに書かれた整数
a
i
(1 ≤
a
i
≤ 100) とその枚数
b
i
(1 ≤
b
i
≤ 10) が空白区切りで与えられます。
続く行にゲームの回数
g
(1 ≤
g
≤ 10)、続く
g
行にゲーム
i
で宣言された整数
n
i
(1 ≤
n
i
≤ 1,000) が与えられます。
データセットの数は 100 を超えない。
Output
入力データセットごとに、
i
行目にゲーム
i
で豪華賞品がもらえるカードの組み合わせ数を出力します。
Sample Input
5
1 10
5 3
10 3
25 2
50 2
4
120
500
100
168
7
1 10
3 10
5 10
10 10
25 10
50 10
100 10
3
452
574
787
0
Output for the Sample Input
16
0
12
7
9789
13658
17466

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int m;
    while(cin >> m, m){
        int n, g;
        vector<int> a(m), b(m);
        vector<ll> dp(7001);
        for(int i = 0; i < m; i++){
            cin >> a[i] >> b[i];
        }
        function<void(int,int)> dfs = [&](int i, int v){
            if(i == m){
                dp[v]++;
                return;
            }
            for(int j = 0; j <= b[i]; j++){
                dfs(i + 1, v + j * a[i]);
            }
        };
        dfs(0, 0);
        cin >> g;
        for(int i = 0; i < g; i++){
            cin >> n;
            cout << dp[n] << '\n';
        }
    }
}
accuracy: 1
time_ms: 210
memory_kb: 3472
final_rank: 10

code_snippet: #include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int Mod=998244353;
const int inf=mod*mod;
const double eps=1e-9;
signed main(){
	int m;
	while(cin>>m,m){
		int a[7],b[7],g,n;
		for(int i=0;i<m;i++)
			cin>>a[i]>>b[i];
		int cnt[7001]={};
		for(int i=0;i<pow(11,m);i++){
			int tmp=i,sum=0;
			bool ok=1;
			for(int j=0;j<m;j++){
				sum+=a[j]*(tmp%11);
				if(tmp%11>b[j])
					ok=0;
				tmp/=11;
			}
			if(ok)
				cnt[sum]++;
		}
		cin>>g;
		for(int i=0;i<g;i++){
			cin>>n;
			cout<<cnt[n]<<endl;
		}
	}
}
accuracy: 1
time_ms: 2600
memory_kb: 3856
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define each(i, a) for(auto &i : a)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#define line cout << "-----------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }

int main() {
    init();

    for (int m; cin >> m && m;) {
        VI a(m), b(m);
        FOR(i, 0, m)cin >> a[i] >> b[i];
        VI x(10001);
        auto dfs = [&](auto&& f, int i, int sum)->void {
            if (i == m)++x[sum];
            else {
                eFOR(j, 0, b[i])f(f, i + 1, sum + a[i] * j);
            }
        };
        dfs(dfs, 0, 0);

        int g; cin >> g;
        FOR(i, 0, g) {
            int n; cin >> n;
            print(x[n]);
        }
    }

    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 3256
final_rank: 5

code_snippet: #include <iostream>

int numOfWays(int a[], int b[], int m, int n, int index)
{
	if (n<0) return 0;
	if (n==0) return 1;
	if (index == m) return 0;
	int res = 0;
	for (int k=0; k<=b[index]; k++) {
		res += numOfWays(a, b, m, n - k*a[index], index+1);
	}
	return res;
}

int main()
{
	int m;
	while (true) {
		std::cin >> m;
		if (m==0) break;
		int a[7], b[7];
		for (int i=0; i<m; i++) std::cin >> a[i] >> b[i];
		int g; std::cin >> g;
		for (int i=0; i<g; i++) {
			int n;
			std::cin >> n;
			std::cout << numOfWays(a, b, m, n, 0) << std::endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 840
memory_kb: 2996
final_rank: 14

code_snippet: #include<iostream>
#include<algorithm>
#include <map>
#include <vector>
#include <stack>
#include<string>
#include<queue>
#include <regex>

using namespace std;

int m;
int count0;

int EstimateMax(int from, pair<int, int> cards[])
{
	int result = 0;
	for (int i = from; i < m; i++)
	{
		result += cards[i].first * cards[i].second;
	}
	return result;
}

void Backtrace(int n, pair<int, int> cards[], int floor)
{
	if (floor == m || n == 0)
	{
		if (n == 0)
			count0++;
		return;
	}
	int value = cards[floor].first;
	int amount = cards[floor].second;
	for (size_t i = 0; i <= amount; i++)
	{
		if (n - value * i >= 0 && EstimateMax(floor + 1,cards) >= (n - value * i))
			Backtrace(n - value * i, cards, floor + 1);
	}
}

int main()
{
	cin >> m;
	while (m != 0)
	{
		pair<int,int> cards[7];
		for (size_t i = 0; i < m; i++)
		{
			cin >> cards[i].first >> cards[i].second;
		}
		int g;
		cin >> g;
		int n;
		for (size_t i = 0; i < g; i++)
		{
			cin >> n;
			count0 = 0;
		    Backtrace(n, cards, 0);
			cout << count0 << endl;
		}
		cin >> m;
	}
	//system("pause");
}
accuracy: 1
time_ms: 320
memory_kb: 3048
final_rank: 7

code_snippet: #include<iostream>
#include<algorithm>
#include <map>
#include <vector>
#include <stack>
#include<string>
#include<queue>
#include <regex>

using namespace std;

int count0;

void Backtrace(int n, pair<int, int> cards[], int floor)
{
	if (floor == -1 || n == 0)
	{
		if (n == 0)
			count0++;
		return;
	}
	int value = cards[floor].first;
	int amount = cards[floor].second;
	for (size_t i = 0; i <= amount; i++)
	{
		if (n - value * i >= 0)
			Backtrace(n - value * i, cards, floor - 1);
	}
}

int main()
{
	int m;
	cin >> m;
	while (m != 0)
	{
		pair<int,int> cards[7];
		for (size_t i = 0; i < m; i++)
		{
			cin >> cards[i].first >> cards[i].second;
		}
		int g;
		cin >> g;
		int n;
		for (size_t i = 0; i < g; i++)
		{
			cin >> n;
			count0 = 0;
			Backtrace(n, cards, m - 1);
			cout << count0 << endl;
		}
		cin >> m;
	}
	//system("pause");
}
accuracy: 1
time_ms: 1340
memory_kb: 3072
final_rank: 17

code_snippet: #include<bits/stdc++.h>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int m, a[10], b[10], n, g, ans[1010];
void f(int sum, int i) {
	if (sum > 1000)return;
	if (i == m) {
		ans[sum]++; return;
	}
	for0(p, b[i] + 1) f(sum + a[i] * p, i + 1);
}
int get() { int r; cin >> r; return r; }
signed main() {
	while (cin >> m, m) {
		for0(i, m)cin >> a[i] >> b[i];
		for0(i, 1001)ans[i] = 0;
		f(0, 0);
		cin >> g;
		while (g--)cout << ans[get()] << endl;
	}
}
accuracy: 1
time_ms: 110
memory_kb: 3012
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <map>
using namespace std;

int main(void){
  int m;
  while(1){
    cin>>m;
    if(m==0)break;
    map<int,int> card;
    vector<int> A;
    int a,b;
    for(int i=0;i<m;i++){
      cin>>a>>b;
      A.push_back(a);
      card[a]=b;
    }
    for(int i=m;i<7;i++){
      A.push_back(0);
      card[0]=0;
    }
    int max=0;
    for(int i=0;i<m;i++){
      max+=A[i]*card[A[i]];
    }
    map<int,int> pattern;
    for(int i=0;i<max+1;i++){
      pattern[i]=0;
    }
    int cost;
    for(int i0=0;i0<=card[A[0]];i0++){
      for(int i1=0;i1<=card[A[1]];i1++){
        for(int i2=0;i2<=card[A[2]];i2++){
          for(int i3=0;i3<=card[A[3]];i3++){
            for(int i4=0;i4<=card[A[4]];i4++){
              for(int i5=0;i5<=card[A[5]];i5++){
                for(int i6=0;i6<=card[A[6]];i6++){
                  cost=i0*A[0]+i1*A[1]+i2*A[2]+i3*A[3]+i4*A[4]+i5*A[5]+i6*A[6];
                  pattern[cost]=pattern[cost]+1;
                }
              }
            }
          }
        }
      }
    }
    int g;
    cin>>g;
    int n;
    for(int i=0;i<g;i++){
      cin>>n;
      cout<<pattern[n]<<endl;
    }
  }
  return 0;

}
accuracy: 1
time_ms: 2380
memory_kb: 3124
final_rank: 19

code_snippet: #include<iostream>
using namespace std;
int a[7],b[7];
int N;
int dfs(int U,int id)
{
	if(id==N)return U==0;
	int ret=0;
	for(int k=0;k<=b[id];k++)
	{
		if(U<k*a[id])break;
		ret+=dfs(U-k*a[id],id+1);
	}
	return ret;
}
main()
{
	while(cin>>N,N)
	{
		for(int i=0;i<N;i++)cin>>a[i]>>b[i];
		int Q;cin>>Q;
		for(;Q--;)
		{
			int g;cin>>g;cout<<dfs(g,0)<<endl;
		}
	}
}
accuracy: 1
time_ms: 450
memory_kb: 3008
final_rank: 11

code_snippet: // 枝狩り無し全探索だと流石にきついか？
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

vector<pair<int,int>> v;
int m, ans;

void dfs(int pos, int res){
    if(res == 0){
        ans++;
        return;
    }
    if(pos == m)    return;
    for(int j = min(v[pos].second, res/v[pos].first); j >= 0; j--){
        dfs(pos+1, res-j*v[pos].first);
    }
}

int main(){
    while(cin >> m, m){
        v.resize(m);
        for(int i = 0; i < m; i++)  cin >> v[i].first >> v[i].second;
        sort(v.rbegin(), v.rend());
        int g, n;
        cin >> g;
        while(g-- > 0){
            cin >> n;
            ans = 0;
            dfs(0, n);
            cout << ans << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 3092
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define Edge pair< i_i, int>
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
 
signed main()
{
    int n;
    while (cin >> n, n) {
        vi data(n), num(n);
        rep(i, n) {
            cin >> data[i] >> num[i];
        }
        vvi dp(n+1, vi(70200, 0));
        dp[0][0] = 1;
        rep(i, n) {
            rep(j, 70200) {
                dp[i+1][j] += dp[i][j];
            }
            Rep(j, num[i]+1) {
                for (int k = 70100; k >= 0; k--) {
                    if (dp[i][k]) {
                        dp[i+1][k+data[i]*j] += dp[i][k];
                    }
                }
            }
        }
        Int(g);
        while (g--) {
            Int(tmp);
            cout << dp[n][tmp] << endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7868
final_rank: 18

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<list>
#include<stack>
#include<queue>
#include<cctype>
#include<iomanip>
#include<functional>
#include<numeric>
#include<map>
#include<set>
#include<complex>
#define EPS 1e-10
using namespace std;
typedef long long llong;
vector<int>ans;
vector<int>a, b;
int m;
void dandc(int x, int i) {
	if (x > 1009)return;
	if (i == m) {
		ans[x]++;
		//cout << "ans[" << x << "] = " << ans[x] << endl;
		return;
	}
	//cout << "i" << " = " << i << endl;
	for (int j = 0; j <= b[i]; j++) {
		dandc(x + j*a[i], i + 1);
	}
}
int main() {
	while (cin >> m&&m) {
		ans = vector<int>(1010, 0);
		a = b = vector<int>(m,0);
		for (int i = 0; i < m; i++) {
			cin >> a[i] >> b[i];
		}
		dandc(0, 0);
		//for (int i = 0; i < 1010; i++) {
		//	cout << i << " : " << ans[i] << endl;
		//}
		int g, n;
		cin >> g;
		while (g--) {
			cin >> n;
			cout << ans[n] << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3160
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define fi first
#define sec second
#define pb push_back
int num[10],cnt[10];
int sum[10000];
int m;
void dfs(int x,int s){
	if(x==m){
		sum[s]++;
		return;
	}
	for(int i=0;i<=cnt[x];i++){
		dfs(x+1,s+num[x]*i);
	}
}
int main(){
	while(1){
		memset(sum,0,sizeof(sum));
		scanf("%d",&m);
		if(m==0)break;
		for(int i=0;i<m;i++){
			scanf("%d %d",&num[i],&cnt[i]);
		}
		dfs(0,0);
		int n,g;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d",&g);
			printf("%d\n",sum[g]);
		}
	}
	return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 1232
final_rank: 2

code_snippet: #include<bits/stdc++.h>
#define M 7
using namespace std;
int m,a[M],b[M],g,n,ans;

void dfs(int x,int sum){
  if(x==m){
    if(sum==n)ans++;
    return;
  }
  for(int i=0;i<=b[x];i++)
    dfs(x+1,sum+a[x]*i);
}

int main(){
  while(1){
    cin>>m;
    if(!m)break;
    for(int i=0;i<m;i++)
      cin>>a[i]>>b[i];
    cin>>g;
    while(g--){
      cin>>n;
      ans=0;
      dfs(0,0);
      cout<<ans<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 1970
memory_kb: 1160
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n,a[10],b[10],ans;
int p;
 
void saiki(int ima,int sum){
  if(sum==p) ans++;
  if(ima==n||sum>=p) return;
  for(int i=0;i<=b[ima];i++) saiki(ima+1,sum+a[ima]*i);
}
 
int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    int m;
    cin>>m;
    while(m--){
      cin>>p;
      ans=0;
      saiki(0,0);
      cout <<ans<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 1070
memory_kb: 1160
final_rank: 8

code_snippet: #include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iomanip>
using namespace std;

int cnt;
int m, num;
int cards[7][2];

void calc(int point, int sum) {
	for(int i=0; i<=cards[point][1]; i++) {
		int temp = sum;
		temp += cards[point][0]*i;
		if(temp == num) {
			cnt++;
			return;
		}
		else {
			if(point != m-1)
				calc(point+1, temp);
		}
	}
}

int main() {
	while(1) {
		cin >> m;
		if(m==0) break;

		for(int i=0; i<m; i++)
			cin >> cards[i][0] >> cards[i][1];

		int g;
		cin >> g;
		for(int i=0; i<g; i++) {
			cnt = 0;
			cin >> num;
			calc(0, 0);
			cout << cnt << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 980
memory_kb: 1160
final_rank: 6

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
#define MAX 7
typedef pair<int,int> P;
 
int m, cnt;
vector<P> v;
 
void check(int num, int pos)
{
    if (num == 0) {
	cnt++;
	return;
    } else if(num < 0 || m <= pos) {
	return;
    }
 
    for (int i = 0; i < v[pos].second; i++) {
	check(num-v[pos].first*(i+1), pos+1);
    }
    check(num, pos+1);
}
 
int main()
{
    int g, num;
    while (cin >> m, m) {
	v.clear(); v.resize(m);
	for (int i = 0; i < m; i++) {
	    cin >> v[i].first >> v[i].second;
	}
 
	cin >> g;
	for (int i = 0; i < g; i++) {
	    cin >> num;
	    cnt = 0; check(num, 0);
	    cout << cnt << endl;
	}
    }
    return 0;
}
accuracy: 1
time_ms: 860
memory_kb: 3076
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n,a[10],b[10],ans;
int p;

void saiki(int ima,int sum){
  if(sum==p) ans++;
  if(ima==n||sum>=p) return;
  for(int i=0;i<=b[ima];i++) saiki(ima+1,sum+a[ima]*i);
}

int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i]>>b[i];
    int m;
    cin>>m;
    while(m--){
      cin>>p;
      ans=0;
      saiki(0,0);
      cout <<ans<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 1100
memory_kb: 1160
final_rank: 9

code_snippet: #include <iostream>
#include <algorithm>
using namespace std ;

int m,n,g;
int card[7][2]; // ??°???????????°

int calc( int cnt , int N ){
	if( N < 0 ) return 0;
	if( N == 0 ) return 1;
	
	if( cnt == m-1 ){
		if( N%card[cnt][0] == 0 && N/card[cnt][0] <= card[cnt][1] ) return 1;
		else return 0;
	}
	
	int ans = 0 ;
	for( int i=0 ; i<=card[cnt][1] ; i++ ){
		ans += calc( cnt+1 , N - (i*card[cnt][0]) ) ;
	}
	
	return ans ;
}

int main(){
	
	while( cin >> m , m ){
		for( int i=0 ; i<m ; i++ ){
			cin >> card[i][0] >> card[i][1];
		}
		for( int i=0 ; i<m ; i++ ){
			for( int j=i+1 ; j<m ; j++ ){
				if( card[i][0] < card[j][0] ){
					swap( card[i] , card[j] ) ;
				}
			}
		}
		
		cin >> g ;
		while( g-- ){
			cin >> n ;
			cout << calc(0,n) << endl ;
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 1160
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define INF (1<<20)
typedef pair<int,int> P;

int m,g;
int card[7],num[7];
int dp[10][10000];
int n;

int solve(int idx,int sum){
  if(sum == n) return 1;
  if(idx == m) return 0;
  if(sum > n) return 0;
  
  int ret = 0;
  for(int i=0;i<=num[idx];i++){
    ret += solve(idx+1,sum+(card[idx]*i));
  }
  return ret;
}

int main(){
  while(cin >> m , m){
    for(int i=0;i<m;i++){
      cin >> card[i] >> num[i];
    }
    cin >> g;
    for(int i=0;i<g;i++){
      cin >> n;
      cout << solve(0,0) << endl;
    }
  }
}
accuracy: 1
time_ms: 1400
memory_kb: 1164
final_rank: 13