Language: C++

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>

using namespace std;

int init, MAX;
int R, C;
bool dp[1010][1 << 13];
int num[1010][1 << 13];
char a[110][110];
int num[300];
int tot, S, P;
int sum[1010];
int nump, p[20], ok[1 << 13];
int x[1010], y[1010];

void pre() {
	memset(ok, 0, sizeof(ok));
	for(int i = 0; i < nump; i++) {
		ok[1 << i] = p[i];
	}
	for(int i = 1; i < (1 << nump); i++) {
		ok[i] = ok[i & (-i)] + ok[i - (i & (-i))];
	}
}

bool solve() {
	memset(dp, 0, sizeof(dp));
	memset(num, 0, sizeof(num));
	dp[0][(1 << nump) - 1] = 1;
	num[0][(1 << nump) - 1] = init;
	for(int i = 0; i < tot; i++) {
		for(int j = 0; j < (1 << nump); j++) {
			if(dp[i][j] == 0) {
				continue;
			}
			int tt = num[i][j];
			tt = min(tt, MAX);
			for(int k = j; k; k = (k-1) & j) {
				if(tt + ok[k] - num[a[x[i+1]][y[i+1]]] > 0) {
					dp[i + 1][j ^ k] = 1;
					num[i + 1][j ^ k] = min(tt + ok[k] - num[a[x[i+1]][y[i+1]]], MAX);
				}
			}
			if(tt - num[a[x[i+1]][y[i+1]]] > 0) {
				dp[i + 1][j] = 1;
				num[i + 1][j] = min(tt + ok[k] - num[a[x[i+1]][y[i+1]]], MAX);
			}
		}
	}
	for(int i = 0; i < (1 << nump); i++) {
		if(dp[tot][i] == 1) {
//			cout << "fuck" << endl;
			return true;
		}
	}
	return false;
}

int main() {
	while(~scanf("%d%d", &init, &MAX)) {
		if(init == 0 && MAX == 0) {
			break;
		}
		scanf("%d%d", &R, &C);
		memset(num, 0, sizeof(num));
		for(int i = 0; i < R; i++) {
			scanf("%s", a[i]);
		}
		int T;
		scanf("%d", &T);
		for(int i = 0; i < T; i++) {
			char tmp[5];
			int id;
			scanf("%s%d", tmp, &id);
			num[tmp[0]] = id;
		}
		scanf("%d", &S);
		tot = 0;
		int tx = 0, ty = 0, inc = 0;
		x[0] = 0; y[0] = 0; sum[0] = 0;
		for(int i = 1; i <= S; i++) {
			char tmp[5];
			int id;
			scanf("%s%d", tmp, &id);
			while(id--) {
				if(tmp[0] == 'U') {
					tx--;
				} else if(tmp[0] == 'D') {
					tx++;
				} else if(tmp[0] == 'L') {
					ty--;
				} else if(tmp[0] == 'R') {
					ty++;
				}
				++tot;
				x[tot] = tx; y[tot] = ty;
				sum[tot] = sum[tot - 1] + num[a[tx][ty]];
			}
		}
		for(int i = 1; i <= tot; i++) {
//			cout << sum[i] << endl;
		}
		scanf("%d", &nump);
		for(int i = 0; i < nump; i++) {
			scanf("%d", &p[i]);
		}
		pre();
		for(int i = 0; i < (1 << nump); i++) {
		//	cout << i << " " << ok[i] << endl;
		}
		if(solve()) {
			puts("YES");
		}else {
			puts("NO");
		}
		for(int i = 0; i <= tot; i++) {
			for(int j = 0; j < (1 << nump); j++) {
//				cout << i << " " << j << " " << dp[i][j] << endl;
			}
		}
	}
	return 0;
}

Compiler output:
a.cc:14:5: error: conflicting declaration 'int num [300]'
   14 | int num[300];
      |     ^~~
a.cc:12:5: note: previous declaration as 'int num [1010][8192]'
   12 | int num[1010][1 << 13];
      |     ^~~
a.cc: In function 'bool solve()':
a.cc:43:47: error: invalid operands of types 'int' and 'int [8192]' to binary 'operator-'
   43 |                                 if(tt + ok[k] - num[a[x[i+1]][y[i+1]]] > 0) {
      |                                    ~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~
      |                                       |                              |
      |                                       int                            int [8192]
a.cc:45:76: error: invalid operands of types 'int' and 'int [8192]' to binary 'operator-'
   45 |                                         num[i + 1][j ^ k] = min(tt + ok[k] - num[a[x[i+1]][y[i+1]]], MAX);
      |                                                                 ~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~
      |                                                                    |                              |
      |                                                                    int                            int [8192]
a.cc:48:31: error: invalid operands of types 'int' and 'int [8192]' to binary 'operator-'
   48 |                         if(tt - num[a[x[i+1]][y[i+1]]] > 0) {
      |                            ~~ ^ ~~~~~~~~~~~~~~~~~~~~~~
      |                            |                         |
      |                            int                       int [8192]
a.cc:50:61: error: 'k' was not declared in this scope
   50 |                                 num[i + 1][j] = min(tt + ok[k] - num[a[x[i+1]][y[i+1]]], MAX);
      |                                                             ^
a.cc: In function 'int main()':
a.cc:79:37: error: incompatible types in assignment of 'int' to 'int [8192]'
   79 |                         num[tmp[0]] = id;
      |                         ~~~~~~~~~~~~^~~~
a.cc:101:57: error: invalid conversion from 'int*' to 'int' [-fpermissive]
  101 |                                 sum[tot] = sum[tot - 1] + num[a[tx][ty]];
      |                                            ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
      |                                                         |
      |                                                         int*