Problem:
タイムセール
より良い物を、より安く。今日もどこかのスーパーマーケットで行われるタイムセールでは、激しい闘いが繰り広げられています。ここ会津にある「LL堂」もそんなスーパーマーケットのひとつで、他のチェーン店と対抗すべく、少し変わったタイムセールを実施しています。一般的なタイムセールでは複数の商品が同じ時間に安くなるものですが、LL堂では対象となる商品によって、タイムセールを開始する時間が別々に設定されています。
坂井家はLL堂をよく利用する家庭の一つです。そんな坂井家では、奥様主導のもと、次の日曜に行われるタイムセールに向けて作戦会議を開き、どのような順番で買い物をすれば値引きが最大となるかを分析することになりました。店内を熟知している坂井家は売り場の見取り図を書き、また欲しい品物がどこにあるか、値引きは幾らであるか、売り切れるまでの時間を把握することに成功しました。
ここまでは完璧だった坂井家ですが、分析を行える人がいませんでした。そこで坂井家と親交があったあなたはプログラムを書くことにしました。一人の人が動くことを想定してシミュレーションを行います。
商品は10種類あり、数字1桁の商品番号
g
によって 区別されます。タイムセール情報には商品番号
g
、 値引き額
d
、タイムセールの開始時刻
s
と売り切れ時刻
e
があります。
店内は横
X
、縦
Y
のマスで構成される2次元 グリッドで表され、マスごとに通路、商品棚の どちらかが割り当てられています。一つの商品棚には 1種類の商品があり、それは商品番号
g
で区別されます。
どの商品を買ってもよいですが、同じ商品番号の商品を複数買ってはいけません。商品棚からは、上下左右に隣接する通路のマスならどんな向きでも商品を取れます。
タイムセールが始まる時刻から商品を取ることができますが、売り切れる時刻からは商品を取ることができません。また、時間は入店した時点で0から始まります。
移動は現在いるマスから上下左右の隣接する通路マスに移動することができますが、商品棚のマスに移動することはできません。グリッドで表される店の外に出ることもできません。１回移動する毎に１単位時間経過します。また、商品を取る時間は考えないものとします。
店内の見取り図と買い物をする人の初期位置と商品のタイムセール情報を入力とし、取ることのできた商品の値引き額の合計の最大値を出力するプログラムを作成してください。
入力
複数のデータセットが与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。
X
Y
m
1,1
m
2,1
...
m
X,1
m
1,2
m
2,2
...
m
X,2
:
m
1,Y
m
2,Y
...
m
X,Y
n
g
1
d
1
s
1
e
1
g
2
d
2
s
2
e
2
:
g
n
d
n
s
n
e
n
1行目に店舗の大きさ
X
,
Y
(3 ≤
X
,
Y
≤ 20) が与えられます。続く
Y
行に
i
列目
j
行目の店内情報
m
i,j
が以下の内容で与えられます。
. (ピリオド) ：通路のマス
数字 ：商品の番号
P ：買い物をする人の初期位置のマス
続く行にタイムセール情報の数
n
(1 ≤
n
≤ 8) が与えられます。続く
n
行に
i
番目のタイムセール情報
g
i
d
i
s
i
e
i
が与えられます(0 ≤
g
i
≤ 9, 1 ≤
d
i
≤ 10000, 0 ≤
s
i
,
e
i
≤ 100)。
データセットの数は 50 を超えません。
出力
データセットごとに、取ることのできる商品の値引き額合計の最大値を１行に出力します。
入力例
6 5
1 1 . 0 0 4
1 . . . . .
. . 2 2 . .
. . 2 2 3 3
P . . . . .
5
0 50 5 10
1 20 0 10
2 10 5 15
3 150 3 5
4 100 8 9
0 0
出力例
180

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using mint = atcoder::modint998244353;
using ld = long double;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}

int dp[20][20][256],np[20][20][256];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    for(int H,W;cin>>W>>H,H!=0;){
        vector A(H,vector<char>(W,0));
        rep(i,H)rep(j,W)cin>>A[i][j];
        int N;
        cin>>N;
        vector<array<int,4>> B(10,{-1,-1,-1,-1}),C(N);
        rep(i,N){
            int g,d,s,e;
            cin>>g>>d>>s>>e;
            B[g]={g,d,s,e};
            C[i]={g,d,s,e};
        }
        sort(all(C));
        int sy,sx;
        rep(i,H)rep(j,W)if(A[i][j]=='P') sy=i,sx=j,A[i][j]='.';
        rep(i,H)rep(j,W){
            if(A[i][j]!='.'){
                if(B[A[i][j]-'0'][1]==-1) A[i][j]='.';
                else{
                    A[i][j]=(char)('0'+(lower_bound(all(C), array<int,4>{A[i][j]-'0', 0, 0, 0})-C.begin()));
                }
            }
        }
        rep(i,H)rep(j,W)rep(k,1<<N) dp[i][j][k]=0;
        dp[sy][sx][0]=1;
        vector<int> dd={0,1,0,-1,0};
        rep(t,101){
            rep(i,H)rep(j,W)rep(k,1<<N) np[i][j][k]=0;

            for(int k=(1<<N);k--;) rep(i,H)rep(j,W){
                rep(l,4){
                    int y=i+dd[l];
                    int x=j+dd[l+1];
                    if(y<0||H<=y||x<0||W<=x) continue;
                    if(A[y][x]!='.'){
                        auto [g,d,s,e]=B[A[y][x]-'0'];
                        if(d==-1) continue;
                        if(t<s||e<=t) continue;
                        dp[i][j][k|(1<<(A[y][x]-'0'))]|=dp[i][j][k];
                    }
                }
            }
            for(int k=(1<<N);k--;) rep(i,H)rep(j,W){
                np[i][j][k]|=dp[i][j][k];
                rep(l,4){
                    int y=i+dd[l];
                    int x=j+dd[l+1];
                    if(y<0||H<=y||x<0||W<=x) continue;
                    if(A[y][x]=='.'){
                        np[y][x][k]|=dp[i][j][k];
                    }
                }
            }
            swap(dp, np);
        }
        int ans=0;
        rep(k,1<<N){
            int b=0;
            rep(i,H)rep(j,W)b|=dp[i][j][k];
            if(!b) continue;
            int wa=0;
            rep(i,N) if((k>>i)&1) wa+=B[i][1];
            chmax(ans, wa);
        }
        cout<<ans<<"\n";
    }

}
accuracy: 1
time_ms: 660
memory_kb: 4352
final_rank: 6

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<climits>
#include<iomanip>
#include<cctype>
#include<sstream>
#include<regex>
#include<bitset>
#include<random>
#include<complex>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
constexpr int mod=1e9+7;
constexpr int Mod=998244353;
constexpr int inf=mod;
constexpr ll linf=(ll)mod*mod;
struct fastio{
	fastio(){
		cin.tie(0);
		ios::sync_with_stdio(0);
		cout<<fixed<<setprecision(20);
	};
}fio;
template<class T>
bool chmax(T&a,const T&b){
	if(a<b){
		a=b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a,const T&b){
	if(a>b){
		a=b;
		return 1;
	}
	return 0;
}
typedef pair<int,int>P;
constexpr int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
int dist[20][20][1024];
int main(){
	int h,w;
	while(cin>>w>>h,h){
		char m[20][20];
		int sx,sy,n,g,d[10]={},s[10]={},e[10]={};
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				cin>>m[i][j];
				if(m[i][j]=='P'){
					sx=i;
					sy=j;
					m[i][j]='.';
				}
			}
		}
		cin>>n;
		for(int i=0;i<n;i++){
			cin>>g;
			cin>>d[g]>>s[g]>>e[g];
			if(s[g]==e[g])
				e[g]=0;
		}
		for(int i=0;i<h;i++)
			for(int j=0;j<w;j++)
				for(int S=0;S<1<<10;S++)
					dist[i][j][S]=inf;
		dist[sx][sy][0]=0;
		priority_queue<P,vector<P>,greater<>>que;
		que.push({0,sx*w+sy<<10});
		int ans=0;
		while(!que.empty()){
			P p=que.top();
			que.pop();
			int x=(p.second>>10)/w,y=(p.second>>10)%w,S=p.second&(1<<10)-1;
			if(dist[x][y][S]<p.first)
				continue;
			int tmp=0;
			for(int i=0;i<10;i++)
				if(S>>i&1)
					tmp+=d[i];
			chmax(ans,tmp);
			for(int i=0;i<4;i++){
				int nx=x+dx[i],ny=y+dy[i];
				if(0<=nx&&nx<h&&0<=ny&&ny<w){
					if(m[nx][ny]=='.'){
						if(dist[nx][ny][S]>dist[x][y][S]+1){
							dist[nx][ny][S]=dist[x][y][S]+1;
							que.push({dist[nx][ny][S],nx*w+ny<<10|S});
						}
					} else{
						int g=m[nx][ny]-'0';
						if(e[g]<=dist[x][y][S])
							continue;
						if(dist[x][y][S|1<<g]>max(dist[x][y][S],s[g])){
							dist[x][y][S|1<<g]=max(dist[x][y][S],s[g]);
							que.push({dist[x][y][S|1<<g],x*w+y<<10|S|1<<g});
						}
					}
				}
			}
		}
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 5188
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int H,W;
char m[20][20];
int dp[2][20][20][1<<10];
int N,D[10],S[10],E[10];
int d[5]={0,1,0,-1};
main()
{
	while(cin>>W>>H,H)
	{
		int now=0;
		for(int i=0;i<H;i++)for(int j=0;j<W;j++)
		{
			cin>>m[i][j];
			for(int k=0;k<1<<10;k++)dp[now][i][j][k]=-1;
			if(m[i][j]=='P')dp[now][i][j][0]=0;
		}
		cin>>N;
		for(int i=0;i<N;i++)
		{
			int g;cin>>g;
			cin>>D[g]>>S[g]>>E[g];
		}
		int ans=0;
		for(int tm=0;tm<100;tm++)
		{
			int nxt=1-now;
			for(int i=0;i<H;i++)for(int j=0;j<W;j++)for(int k=0;k<1<<10;k++)dp[nxt][i][j][k]=-1;
			for(int i=0;i<H;i++)for(int j=0;j<W;j++)for(int k=0;k<1<<10;k++)
			{
				int cost=dp[now][i][j][k];
				if(cost<0)continue;
				int nk=k;
				for(int r=0;r<4;r++)
				{
					int x=i+d[r],y=j+d[r+1];
					if(x>=0&&y>=0&&x<H&&y<W&&'0'<=m[x][y]&&m[x][y]<='9')
					{
						int id=m[x][y]-'0';
						if(S[id]<=tm&&tm<E[id]&&~nk>>id&1)
						{
							nk|=1<<id;
							cost+=D[id];
						}
					}
				}
				if(ans<cost)ans=cost;
				for(int r=0;r<4;r++)
				{
					int x=i+d[r],y=j+d[r+1];
					if(x>=0&&y>=0&&x<H&&y<W&&!('0'<=m[x][y]&&m[x][y]<='9'))
					{
						dp[nxt][x][y][nk]=max(dp[nxt][x][y][nk],cost);
					}
				}
			}
			now=nxt;
		}
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 630
memory_kb: 6272
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <assert.h>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

bool dp[2][20][20][1 << 10];

int main() {
    int h, w;
    array<int, 5> dy{-1, 1, 0, 0, 0}, dx{0, 0, -1, 1, 0};
    while(cin >> w >> h, w){
        vector<vector<int>> grid(h+2, vector<int>(w+2, -1));
        int sy = 0, sx = 0;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                char c; cin >> c;
                if(c == 'P') sy = i, sx = j, grid[i+1][j+1] = 0;
                else if(c == '.') grid[i+1][j+1] = 0;
                else grid[i+1][j+1] = 1+(c-'0');
            }
        }
        int n;
        cin >> n;
        vector<int> gs(n), ds(n), ss(n), es(n);
        int M = 110;
        vector<int> id(11, -1);
        for (int i = 0; i < n; ++i) {
            cin >> gs[i] >> ds[i] >> ss[i] >> es[i];
            assert(id[gs[i]+1] == -1);
            id[gs[i]+1] = i;
        }
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                for (int k = 0; k < 1 << n; ++k) {
                    dp[0][i][j][k] = 0;
                }
            }
        }
        dp[0][sy][sx][0] = 1;
        for (int i = 0; i < M; ++i) {
            int now = i&1, nxt = now^1;
            for (int j = 0; j < h; ++j) {
                for (int k = 0; k < w; ++k) {
                    for (int l = 0; l < 1 << n; ++l) {
                        dp[nxt][j][k][l] = 0;
                    }
                }
            }
            for (int j = 0; j < h; ++j) {
                for (int k = 0; k < w; ++k) {
                    for (int d = 0; d < 5; ++d) {
                        if(grid[j+1+dy[d]][k+1+dx[d]]) continue;
                        for (int l = 0; l < (1 << n); ++l) {
                            if(!dp[now][j][k][l]) continue;
                            int l2 = l;
                            for (int dd = 0; dd < 4; ++dd) {
                                if(!~grid[j+1+dy[dd]][k+1+dx[dd]] || !~id[grid[j+1+dy[dd]][k+1+dx[dd]]]) continue;
                                int m = id[grid[j+1+dy[dd]][k+1+dx[dd]]];
                                if(!(l2 & (1 << m)) && ss[m] <= i && i < es[m]) {
                                    l2 |= (1 << m);
                                }
                            }
                            dp[nxt][j+dy[d]][k+dx[d]][l2] = 1;
                        }
                    }
                }
            }
        }
        int ans = 0;
        for (int j = 0; j < h; ++j) {
            for (int k = 0; k < w; ++k) {
                for (int l = 0; l < (1 << n); ++l) {
                    if(!dp[M&1][j][k][l]) continue;
                    int val = 0;
                    for (int m = 0; m < n; ++m) {
                        if(l & (1 << m)) val += ds[m];
                    }
                    ans = max(ans, val);
                }
            }
        }
        cout << ans << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 330
memory_kb: 3852
final_rank: 5

code_snippet: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

char board[30][30];
int H, W, disc[10][105], rec[20][20][105][1 << 8];

struct Elem {
    int x, y, time, bit, cost;
    bool operator<(const Elem &e) const {
        return cost < e.cost;
    }
};
 
signed main() {
    while(cin >> W >> H, H || W) {
        memset(disc, 0, sizeof(disc));
        memset(rec, -1, sizeof(rec));
        int sx = -1, sy = -1;
        for(int i=0; i<H; i++) {
            for(int j=0; j<W; j++) {
                cin >> board[i][j];
                if(board[i][j] == 'P') sx = i, sy = j;
            }
        }

        int N, lim = 0, ans = 0; cin >> N;
        vector<int> comp(10, -1);
        for(int i=0; i<N; i++) {
            int g, d, s, e; cin >> g >> d >> s >> e;
            comp[g]++;
            chmax(lim, e + 1);
            for(int k=s; k<e; k++) {
                chmax(disc[g][k], d);
            }
        }

        // ざあつ
        int id = 0;
        for(auto &x : comp) {
            if(x >= 0) x = id++;
        }
        vector<int> inv(id);
        for(int i=0; i<10; i++) {
            if(comp[i] < 0) continue;
            inv[ comp[i] ] = i;
        }

        // cand の部分集合
        vector< vector<int> > sum(1 << id, vector<int>(lim));
        for(int bit=0; bit<(1<<id); bit++) {
            for(int i=0; i<id; i++) {
                if(bit >> i & 1) {
                    int orig_id = inv[i];
                    for(int t=0; t<lim; t++) {
                        sum[bit][t] += disc[orig_id][t];
                    }
                }
            }
        }

        priority_queue<Elem> que;
        que.push(Elem{sx, sy, 0, 0, 0});
        rec[sx][sy][0][0] = 0;

        while(que.size()) {
            Elem cur = que.top(); que.pop();
            int x = cur.x, y = cur.y, bit = cur.bit, time = cur.time, cost = cur.cost;
            chmax(ans, cost);
            if(cost < rec[x][y][time][bit] || time >= lim) continue;

            // 商品を取る
            int cand = 0;
            for(int k=0; k<4; k++) {
                int mx = x + dx[k], my = y + dy[k];
                if(!(mx < 0 || mx >= H || my < 0 || my >= W) and ('0' <= board[mx][my] && board[mx][my] <= '9')) {
                    int c = comp[ board[mx][my] - '0' ]; // 圧縮する
                    if(bit >> c & 1) continue;
                    cand |= (1 << c);
                }
            }

            for(int mask=cand; mask>=0; mask=(mask-1)&cand) {
                int nbit = bit | mask;
                int ncost = cost + sum[mask][time];

                // 移動先
                for(int k=0; k<4; k++) {
                    int nx = x + dx[k], ny = y + dy[k], ntime = time + 1;
                    if(!(nx < 0 || nx >= H || ny < 0 || ny >= W) and !('0' <= board[nx][ny] && board[nx][ny] <= '9') and (rec[nx][ny][ntime][nbit] < ncost)) {
                        rec[nx][ny][ntime][nbit] = ncost;
                        que.push(Elem{nx, ny, ntime, nbit, ncost});
                    }
                }

                if(mask == 0) break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 4580
memory_kb: 92248
final_rank: 20

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld =long double;
const ld eps = 1e-9;

bool check(const vector<vector<pair<int, int>>>&humans, int x, int y) {
	for (int i = 0; i < 4; ++i) {
		auto it=find(humans[i].begin(),humans[i].end(),make_pair(x,y));
		if(it!=humans[i].end())return false;
	}
	return true;
}
int dx[] = { -1,0,1,0,0 };
int dy[] = { 0,1,0,-1,0 };

template<typename T> struct Compress {
	map<T, int>mp;
	map<int, T>revmp;

	Compress(vector<T>vs) {
		setmp(vs);
	}

	Compress() :mp(), revmp() {

	}
	void setmp(vector<T>vs) {
		sort(vs.begin(), vs.end());
		vs.erase(unique(vs.begin(), vs.end()), vs.end());
		for (int i = 0; i < static_cast<int>(vs.size()); ++i) {
			mp[vs[i]] = i;
			revmp[i] = vs[i];
		}
	}

};
struct sale {
	int id;
	int d;
	int s;
	int e;
};

struct aa {
	int flag;
	int x;
	int y;
	int cost;
};


int main()
{
	while (true) {
		int W,H;cin>>W>>H;
		if(!W)break;
		vector<vector<int>>field(H,vector<int>(W,-1));
		vector<sale>aas;
		int sx,sy;
		{
			for (int i = 0; i < H; ++i) {
				for (int j = 0; j < W; ++j) {
					char ch;cin>>ch;
					if (ch == '.') {

					}
					else if (ch == 'P') {
						sx = j;
						sy = i;
					}
					else {
						assert(isdigit(ch));
						field[i][j] = ch - '0';
					}
				}
			}
			int N; cin >> N;
			Compress<int> comp;

			vector<int>v;

			for (int i = 0; i < N; ++i) {
				int g, d, s, e; cin >> g >> d >> s >> e;
				v.push_back(g);
				aas.push_back(sale{ g,d,s,e });
			}
			comp.setmp(v);
			for (auto & line : field) {
				for (auto&n : line) {
					if (n >= 0) {
						if (comp.mp.find(n) != comp.mp.end()) {
							n = comp.mp[n];
						}
						else {
							n=-3;
						}
					}
				}
			}

			vector<sale>real_aas(comp.mp.size(), sale{ -1,0,0,0 });
			for (auto aaa : aas) {
				real_aas[comp.mp[aaa.id]]=(sale{ aaa.id,aaa.d,aaa.s,aaa.e });
			}
			aas=real_aas;
		}
		vector<vector<vector<int>>>memo(H,vector<vector<int>>(W,vector<int>(1<<aas.size(),-1e9)));
		memo[sy][sx][0]=0;

		for (int time = 0; time < 110; ++time) {

			vector<vector<vector<int>>>next_memo(H, vector<vector<int>>(W, vector<int>(1 << aas.size(), -1e9)));
			for (int y = 0; y < H; ++y) {
				for (int x = 0; x < W; ++x) {
					for (int flag = 0; flag < (1 << aas.size()); ++flag) {

						for (int way = 0; way < 4; ++way) {
							const int next_y=y+dy[way];
							const int next_x=x+dx[way];
							if(next_x<0||W<=next_x||next_y<0||H<=next_y)continue;
							else {
								const int k=field[next_y][next_x];
								if (k >= 0&&(flag&(1<<k))==0) {
									
									sale asale(aas[k]);
									if (asale.s <= time&&time < asale.e) {
										memo[y][x][flag|(1<<k)]=max(memo[y][x][flag|(1<<k)],memo[y][x][flag]+asale.d);
									}
								}
							}
						}

						for (int way = 0; way < 5; ++way) {
							const int next_y = y + dy[way];
							const int next_x = x + dx[way];
							if (next_x<0 || W <= next_x || next_y<0 || H <= next_y)continue;
							else {
								const int k = field[next_y][next_x];
								if (k == -1) {
									next_memo[next_y][next_x][flag]=max(next_memo[next_y][next_x][flag],memo[y][x][flag]);
								}
							}
						}
					}
				}
			}
			memo=next_memo;
		}
		int ans=0;
		for (int y = 0; y < H; ++y) {
			for (int x = 0; x < W; ++x) {
				for (int flag = 0; flag < (1 << aas.size()); ++flag) {
					ans=max(ans,memo[y][x][flag]);
					
				}
			}
		}
		cout<<ans<<endl;
		
	}
	return 0;
}
accuracy: 1
time_ms: 1070
memory_kb: 3784
final_rank: 9

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Cell{
	Cell(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	bool operator<(const struct Cell &arg) const{
		if(row != arg.row){
			return row < arg.row;
		}else{
			return col < arg.col;
		}
	};
	int row,col;
};

struct Info{
	int discount,start,end;
};

struct Data{
	Data(int arg_row,int arg_col,int arg_total_dist){
		row = arg_row;
		col = arg_col;
		total_dist = arg_total_dist;
	}

	int row,col,total_dist;
};

struct SALE{
	SALE(){
		state = time = row = col = sum_discount = 0;
	}

	SALE(int arg_state,int arg_time,int arg_row,int arg_col,int arg_sum_discount){
		state = arg_state;
		time = arg_time;
		row = arg_row;
		col = arg_col;
		sum_discount = arg_sum_discount;
	}

	int state,time,row,col,sum_discount;
};

int W,H;
int POW[11];
int dp[1024][20][20];
Info info[10];
bool is_sale[10];
int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};

char base_map[20][21];

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}



void func(){

	char buf[2];

	int start_row,start_col;

	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){
			scanf("%s",buf);
			base_map[row][col] = buf[0];
			if(buf[0] == 'P'){
				base_map[row][col] = '.';
				start_row = row;
				start_col = col;
			}
		}
	}

	int N;
	scanf("%d",&N);

	int g,d,s,e;

	for(int i = 0; i < 10; i++)is_sale[i] = false;

	for(int loop = 0; loop < N; loop++){
		scanf("%d %d %d %d",&g,&d,&s,&e);

		info[g].discount = d;
		info[g].start = s;
		info[g].end = e;

		is_sale[g] = true;
	}

	int limit = POW[10];

	for(int state = 0; state < limit; state++){
		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++){
				dp[state][row][col] = -1;
			}
		}
	}

	queue<SALE> Q;

	dp[0][start_row][start_col] = 0;

	SALE first;
	first.row = start_row;
	first.col = start_col;
	first.state = 0;
	first.sum_discount = 0;
	first.time = 0;

	Q.push(first);
	int max_value = 0;

	while(!Q.empty()){

		max_value = max(max_value,Q.front().sum_discount);

		for(int i = 0; i < 4; i++){
			int adj_row = Q.front().row + diff_row[i];
			int adj_col = Q.front().col + diff_col[i];

			if(rangeCheck(adj_row,adj_col) == false)continue;

			if(base_map[adj_row][adj_col] == '.'){

				if(dp[Q.front().state][adj_row][adj_col] < Q.front().sum_discount){
					dp[Q.front().state][adj_row][adj_col] = Q.front().sum_discount;

					SALE next_sale;
					next_sale.row = adj_row;
					next_sale.col = adj_col;
					next_sale.state = Q.front().state;
					next_sale.sum_discount = Q.front().sum_discount;
					next_sale.time = Q.front().time+1;

					Q.push(next_sale);
				}
			}else if(base_map[adj_row][adj_col] >= '0' && base_map[adj_row][adj_col] <= '9'){

				int adj_item = base_map[adj_row][adj_col]-'0';

				if(Q.front().state & (1 << adj_item))continue;
				if(is_sale[adj_item] == false)continue;

				int next_state = Q.front().state + POW[adj_item];

				if(Q.front().time >= info[adj_item].start && Q.front().time < info[adj_item].end){

					if(dp[next_state][Q.front().row][Q.front().col] < Q.front().sum_discount+info[adj_item].discount){

						dp[next_state][Q.front().row][Q.front().col] = Q.front().sum_discount+info[adj_item].discount;

						SALE next_sale;
						next_sale.row = Q.front().row;
						next_sale.col = Q.front().col;
						next_sale.state = next_state;
						next_sale.sum_discount = Q.front().sum_discount+info[adj_item].discount;
						next_sale.time = Q.front().time;

						Q.push(next_sale);

					}

				}else if(Q.front().time < info[adj_item].start){

					SALE next_sale;
					next_sale.row = Q.front().row;
					next_sale.col = Q.front().col;
					next_sale.state = Q.front().state;
					next_sale.sum_discount = Q.front().sum_discount;
					next_sale.time = info[adj_item].start;

					Q.push(next_sale);
				}
			}
		}
		Q.pop();
	}

	printf("%d\n",max_value);
}


int main(){

	for(int i = 0; i <= 10; i++)POW[i] = pow(2,i);

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5136
final_rank: 3

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Cell{
	Cell(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	bool operator<(const struct Cell &arg) const{
		if(row != arg.row){
			return row < arg.row;
		}else{
			return col < arg.col;
		}
	};
	int row,col;
};

struct Info{
	int discount,start,end;
};

struct Data{
	Data(int arg_row,int arg_col,int arg_total_dist){
		row = arg_row;
		col = arg_col;
		total_dist = arg_total_dist;
	}

	int row,col,total_dist;
};

struct SALE{
	SALE(){
		state = time = row = col = sum_discount = 0;
	}

	SALE(int arg_state,int arg_time,int arg_row,int arg_col,int arg_sum_discount){
		state = arg_state;
		time = arg_time;
		row = arg_row;
		col = arg_col;
		sum_discount = arg_sum_discount;
	}

	int state,time,row,col,sum_discount;
};

int W,H;
int POW[11];
int dp[1024][20][20];
Info info[10];
bool is_sale[10];
int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};

char base_map[20][21];

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}



void func(){

	char buf[2];

	int start_row,start_col;

	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){
			scanf("%s",buf);
			base_map[row][col] = buf[0];
			if(buf[0] == 'P'){
				base_map[row][col] = '.';
				start_row = row;
				start_col = col;
			}
		}
	}

	int N;
	scanf("%d",&N);

	int g,d,s,e;

	for(int i = 0; i < 10; i++)is_sale[i] = false;

	for(int loop = 0; loop < N; loop++){
		scanf("%d %d %d %d",&g,&d,&s,&e);

		info[g].discount = d;
		info[g].start = s;
		info[g].end = e;

		is_sale[g] = true;
	}

	int limit = POW[10];

	for(int state = 0; state < limit; state++){
		for(int row = 0; row < H; row++){
			for(int col = 0; col < W; col++){
				//dp[state][row][col] = BIG_NUM; //????????????????????????
				dp[state][row][col] = -1;
			}
		}
	}

	queue<SALE> Q;

	dp[0][start_row][start_col] = 0;

	SALE first;
	first.row = start_row;
	first.col = start_col;
	first.state = 0;
	first.sum_discount = 0;
	first.time = 0;

	Q.push(first);
	int max_value = 0;

	while(!Q.empty()){

		//printf("row:%d col:%d time:%d sum:%d state:%d\n",Q.front().row,Q.front().col,Q.front().time,Q.front().sum_discount,Q.front().state);

		max_value = max(max_value,Q.front().sum_discount);

		for(int i = 0; i < 4; i++){
			int adj_row = Q.front().row + diff_row[i];
			int adj_col = Q.front().col + diff_col[i];

			if(rangeCheck(adj_row,adj_col) == false)continue;

			if(base_map[adj_row][adj_col] == '.'){ //???????????´???

				if(dp[Q.front().state][adj_row][adj_col] < Q.front().sum_discount){
					dp[Q.front().state][adj_row][adj_col] = Q.front().sum_discount;

					SALE next_sale;
					next_sale.row = adj_row;
					next_sale.col = adj_col;
					next_sale.state = Q.front().state;
					next_sale.sum_discount = Q.front().sum_discount;
					next_sale.time = Q.front().time+1;

					//printf("?????? row:%d col:%d?????????%d state:%d??§????§?\n",adj_row,adj_col,Q.front().time+1,Q.front().state);

					Q.push(next_sale);
				}
			}else if(base_map[adj_row][adj_col] >= '0' && base_map[adj_row][adj_col] <= '9'){

				int adj_item = base_map[adj_row][adj_col]-'0';

				if(Q.front().state & (1 << adj_item))continue;
				if(is_sale[adj_item] == false)continue;

				int next_state = Q.front().state + POW[adj_item];

				if(Q.front().time >= info[adj_item].start && Q.front().time < info[adj_item].end){ //??????????§??????????????????§?????????????????????

					if(dp[next_state][Q.front().row][Q.front().col] < Q.front().sum_discount+info[adj_item].discount){

						dp[next_state][Q.front().row][Q.front().col] = Q.front().sum_discount+info[adj_item].discount;

						SALE next_sale;
						next_sale.row = Q.front().row;
						next_sale.col = Q.front().col;
						next_sale.state = next_state;
						next_sale.sum_discount = Q.front().sum_discount+info[adj_item].discount;
						next_sale.time = Q.front().time;

						//printf("?????? row:%d col:%d?????????%d state:%d??§????§?\n",adj_row,adj_col,Q.front().time+1,Q.front().state);

						Q.push(next_sale);

					}

				}else if(Q.front().time < info[adj_item].start){

					/*if(dp[next_state][Q.front().row][Q.front().col] < Q.front().sum_discount+info[adj_item].discount){

						dp[next_state][Q.front().row][Q.front().col] = Q.front().sum_discount+info[adj_item].discount;*/
						SALE next_sale;
						next_sale.row = Q.front().row;
						next_sale.col = Q.front().col;
						next_sale.state = Q.front().state;
						next_sale.sum_discount = Q.front().sum_discount;
						next_sale.time = info[adj_item].start;

						//printf("?????\?????? row:%d col:%d?????????%d state:%d??§????§?\n",adj_row,adj_col,Q.front().time+1,Q.front().state);

						Q.push(next_sale);
					//}
				}
			}
		}
		Q.pop();
	}

	printf("%d\n",max_value);
}


int main(){

	for(int i = 0; i <= 10; i++)POW[i] = pow(2,i);

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5076
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < (int)(n) ; i++)
static const int INF = (1 << 29);
static const int dx[] = {1,-1,0,0};
static const int dy[] = {0,0,1,-1};

struct State{
   int x,y,t,sum,unvisit;
   State(int x, int y, int t, int s, int v):x(x),y(y),t(t),sum(s),unvisit(v){};
};

struct Goods{
   int price,s,e;
   Goods(){}
   Goods(int p, int s, int e):price(p),s(s),e(e){};
};

int h,w,n;
int sx,sy;
int G[20][20];
Goods goods[10];
bool visited[20][20][(1 << 10)][101];

int bfs(){
   memset(visited,false,sizeof(visited));  int ans = 0;
   queue<State> q;
   q.push(State(sx, sy, 0, 0, (1<<10)-1));

   while(!q.empty()){
      State s = q.front(); q.pop();
      if(s.t > 100)continue;
      if(visited[s.x][s.y][s.unvisit][s.t])continue;
      visited[s.x][s.y][s.unvisit][s.t] = true;
      ans = max(ans, s.sum);
      rep(k,4){
    	 int nx = s.x + dx[k];int ny = s.y + dy[k];
    	 if(0<=nx&&nx<h && 0<=ny&&ny<w && G[nx][ny]!=-1){
	     int num = G[nx][ny];
	     if((s.unvisit >> num) & 1){
	        if(goods[num].s <= s.t && s.t < goods[num].e){
		  s.sum += goods[num].price;
		  s.unvisit = s.unvisit & ~(1 << num);
	       }
	    }
	   }
      }

     rep(k,4){
	 int nx = s.x + dx[k]; int ny = s.y + dy[k];
	 if(0<=nx&&nx<h && 0<=ny&&ny<w && G[nx][ny]==-1){
	    q.push(State(nx, ny, s.t+1, s.sum, s.unvisit));
	 }
      }
   }
   return ans;
}

void input(){
   rep(i,h){rep(j,w){char c;cin >> c;
	 if(c=='P'){sx = i;sy = j;G[i][j] = -1;}
	 else if(c=='.')G[i][j] = -1; else G[i][j] = (int)(c - '0');}
   }
   cin >> n;
   rep(i,n){int g,d,s,e; cin >> g >> d >> s >> e;
      goods[g] = Goods(d,s,e);
   }
}

int main(){
   while(cin >> w >> h){
      if(w==0 && h==0)break;
      input();
      cout << bfs() << endl;
   }
   return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 45452
final_rank: 15

code_snippet: #include <iostream>
#include <cstring>
#include <vector>

using namespace std;

#define MAX_H 20
#define MAX_W 20
#define MAX_T 150

struct Data{
    int n,c,s,t;
    Data(){}
    Data(int n,int c,int s,int t) :
        n(n),c(c),s(s),t(t) {}

    bool operator < (const Data &d)const{
        return n < d.n;
    }
};

int H,W,LIMIT;
int memo[MAX_H][MAX_W][MAX_T][1<<8];
vector<Data> v;
char field[MAX_H][MAX_W];
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};

bool inField(int x,int y){
    return (0 <= x && x < W && 0 <= y && y < H);
}

bool check(int nt,int idx){
    return (v[idx].s <= nt && nt < v[idx].t);
}

int solve(int x,int y,int t,int p){
    if(t == LIMIT) return 0;
    int &res = memo[x][y][t][p];
    if(res != -1) return res;
    int S = p,cost = 0;
    for(int i = 0 ; i < 4 ; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(!inField(nx,ny)) continue;
        if(field[ny][nx] == '.') continue;
        int x = field[ny][nx]-'0';
        if(S >> x & 1) continue;
        if(check(t,x)){
            S |= (1<<x);
            cost += v[x].c;
        }
    }
    for(int i = 0 ; i < 4 ; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(!inField(nx,ny)) continue;
        if(field[ny][nx] != '.') continue;
        res = max(res,solve(nx,ny,t+1,S)+cost);
    }
    return res;
}

int main(){
    int sx,sy,N;
    while(cin >> W >> H, W){
        for(int i = 0 ; i < H ; i++){
            for(int j = 0 ; j < W ; j++){
                cin >> field[i][j];
                if(field[i][j] == 'P'){
                    sx = j; sy = i;
                    field[i][j] = '.';
                }
            }
        }
        LIMIT = 0;
        cin >> N;
        v.clear(); v.resize(N);
        for(int i = 0 ; i < N ; i++){
            cin >> v[i].n >> v[i].c >> v[i].s >> v[i].t;
            LIMIT = max(LIMIT,v[i].t);
        }
        LIMIT++;
        memset(memo,-1,sizeof(memo));
        cout << solve(sx,sy,0,0) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 63100
final_rank: 18

code_snippet: #include <iostream>
using namespace std;
struct comd { int sn,d,s,e; };
int main() {
  int x,y,i,j,k,g,n,t,a,b,e,h,dx[4]={1,0,-1,0},dy[4]={0,-1,0,1},d[2][20][20][256],m[20][20],f[20][20];
  char c;
  comd co[10];
  while(cin >> x >> y && x>0) {
	  for (h=0;h<2;h++) for (j=0;j<20;j++) for (k=0;k<20;k++) for (g=0;g<256;g++) d[h][j][k][g]=-1;
	  for (i=0;i<y;i++) {
		  for (j=0;j<x;j++) {
			  cin >> c;  
			  f[i][j]=0;
			  if (c>='0' && c<='9') m[i][j]=c-'0'; else m[i][j]=-1;
			  if (c=='P') { f[i][j]=1; d[0][i][j][0]=0;}
		  }
	  }
	  cin >> n;
	  for (i=0; i<n; i++) {  cin >> g;  cin >> co[g].d >> co[g].s >> co[g].e; co[g].sn=(1 << i);}
	  for (t=0,h=0;t<=100;t++) {
		  for (i=0;i<y;i++) for (j=0;j<x;j++) {
			  if (f[i][j]==0) continue;
		      for (k=0;k<256;k++) {
				if (d[h][i][j][k]==-1) continue;
				a=k; b=d[h][i][j][k];
				for (g=0;g<4;g++) {
					if (j+dx[g]<0 || j+dx[g]>=x || i+dy[g]<0 || i+dy[g]>=y) continue;
					e=m[i+dy[g]][j+dx[g]];
					if (e>=0) if ((co[e].sn & a)==0 && co[e].s<=t && co[e].e>t) {b+=co[e].d; a=a | co[e].sn;}
				}
				d[h][i][j][a]=b;
				for (g=0;g<4;g++) if (j+dx[g]>=0 && j+dx[g]<x && i+dy[g]>=0 && i+dy[g]<y) if (m[i+dy[g]][j+dx[g]]<0) { d[1-h][i+dy[g]][j+dx[g]][a]=b;  f[i+dy[g]][j+dx[g]]=1;} 
				}
			}
			h=1-h;
		}
		a=-1;
		for (i=0;i<y;i++) for (j=0;j<x;j++) for (k=0;k<256;k++) if (a<d[0][i][j][k]) a=d[0][i][j][k];
		cout << a << endl;
	  }
 return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 1964
final_rank: 1

code_snippet: #include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
#include <complex>
using namespace std;
const int INF = 1<<29;
const double EPS = 1e-8;
//typedef vector<int> vec;
typedef vector<char> vec;
typedef vector<vec> mat;
typedef pair<int,int> P;
struct edge{int to,cost;};

struct State{
    int t,get,y,x;
};
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};

bool visited[100][(1<<10)][20][20];

int main(){
    int w, h;
    while(cin >> w >> h, w){
        mat field(h, vec(w));
        int sx,sy;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> field[i][j];
                if(field[i][j]=='P'){
                    sx = j; sy = i;
                }
            }
        }

        int de_sum[(1<<10)];
        fill(de_sum, de_sum+(1<<10), 0);
        int n;
        cin >> n;
        int st_i[10], et_i[10];
        fill(st_i,st_i+10,INF);
        fill(et_i,et_i+10,-INF);
        for(int i=0;i<n;i++){
            int go, de, st, et;
            scanf("%d%d%d%d", &go, &de, &st, &et);
            st_i[go] = st; et_i[go] = et;
            for(int j=1;j<(1<<10);j++){
                if((j>>go)&1){
                    de_sum[j] += de;
                }
            }
        }

        memset(visited, 0, sizeof(visited));
        queue<State> que;
        que.push((State){0,0,sy,sx});
        int ans = 0;
        while(que.size()){
            State p = que.front(); que.pop();
            //printf("p.t=%d p.get=%d p.y=%d p.x=%d\n",p.t,p.get,p.y,p.x);
            for(int j=0;j<4;j++){
                int ny = p.y + dy[j], nx = p.x + dx[j];
                if(ny<0||h<=ny||nx<0||w<=nx)continue;
                if('0'<=field[ny][nx]&&field[ny][nx]<='9'){
                    int g_i = field[ny][nx] - '0';
                    if(st_i[g_i] <= p.t && p.t < et_i[g_i]){
                        p.get |= (1<<g_i);
                    }
                }
            }
            if(visited[p.t][p.get][p.y][p.x]) continue;
            visited[p.t][p.get][p.y][p.x] = true;
            ans = max(ans, de_sum[p.get]);
            for(int i=0;i<4;i++){
                int ny = p.y + dy[i], nx = p.x + dx[i];
                if(ny<0||h<=ny||nx<0||w<=nx) continue;
                if(field[ny][nx]=='.'||field[ny][nx]=='P'){
                    if(p.t+1<100&&!visited[p.t+1][p.get][ny][nx]){
                        que.push((State){p.t+1, p.get, ny, nx});
                    }
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 42968
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define Rep(i, s, t) for (int i = (int)(s); i <= (int)(t); i++)
#define rep(i, n) Rep(i, 0, (n) - 1)
const int INF = 1 << 26;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int h, w;
int sx, sy;
char grid[22][22];
int sale[10][100];
int dp[101][22][22][1 << 8];
vector<int> salev;
int toid[10];

int main()
{
	while (scanf("%d %d", &w, &h), w){
		fill_n(*sale, 10 * 100, -INF);
		fill_n(*grid, 22 * 22, '\0');
		fill_n(toid, 10, -1);
		salev.clear();
		
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				scanf(" %c", &grid[i][j]);
				if (grid[i][j] == 'P'){
					sx = i;
					sy = j;
					grid[i][j] = '.';
				}
			}
		}
		
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; i++){
			int g, d, s, e;
			scanf("%d %d %d %d", &g, &d, &s, &e);
			for (int j = s; j < e; j++){
				sale[g][j] = max(sale[g][j], d);
			}
			salev.push_back(g);
		}
		
		sort(salev.begin(), salev.end());
		salev.erase(unique(salev.begin(), salev.end()), salev.end());
		
		for (int i = 0; i < salev.size(); i++){
			toid[salev[i]] = i;
		}
		
		fill_n(***dp, 101 * 22 * 22 * (1 << 8), -INF);
		dp[0][sx][sy][0] = 0;
		rep (t, 100) Rep (x, 1, h) Rep (y, 1, w){
			rep (got, 1 << salev.size()){
				if (dp[t][x][y][got] == -INF) continue;
				if (grid[x][y] != '.') continue;
				//if (dp[t][x][y][got] > 0) printf("%d\n", dp[t][x][y][got]);
				//printf("%d %d %d %d\n", t, x, y, got);
				//dp[t + 1][x][y][got] = max(dp[t + 1][x][y][got], dp[t][x][y][got]);
				rep (dir, 4){
					int nx = x + dx[dir];
					int ny = y + dy[dir];
					if (grid[nx][ny] >= '0' && grid[nx][ny] <= '9'){
						int g = grid[nx][ny] - '0';
						int id = toid[g];
						if (id != -1 && !(got & (1 << id))){
							dp[t][x][y][got | (1 << id)] = max(
								dp[t][x][y][got | (1 << id)],
								dp[t][x][y][got] + sale[g][t]
							);
						}
					}
					dp[t + 1][nx][ny][got] = max(
						dp[t + 1][nx][ny][got],
						dp[t][x][y][got]
					);
				}
			}
		}
		
		int res = 0;
		rep (t, 100) Rep (x, 1, h) Rep(y, 1, w) rep(st, 1 << salev.size()){
			res = max(res, dp[t][x][y][st]);
		}
		
		printf("%d\n", res);
	}
}
accuracy: 1
time_ms: 480
memory_kb: 50100
final_rank: 16

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

struct pos {
	int x, y;
	int c;

	pos() {}
	pos(int x_, int y_, int c_) {
		x = x_; y = y_; c = c_;
	}
};

bool visited[22][22][105][1024];

int main() {
	int x, y;
	while(cin >> x >> y, x || y) {
		vector<string> shop;
		string str;
		int sale[10][3];
		int mx_t = 0;
		for(int i = 0; i < 22*22*105*1024; i++)
			visited[0][0][0][i] = false;
		for(int i = 0; i < x+2; i++)
			str += "#";
		shop.push_back(str);
		cin.ignore();
		for(int i = 0; i < y; i++) {
			string tmp;
			tmp = "#";
			for(int j = 0; j < x; j++) {
				char in;
				cin >> in; cin.ignore();
				tmp += in;
			}
			tmp += "#";
			shop.push_back(tmp);
		}
		shop.push_back(str);

		int n;
		cin >> n;
		for(int i = 0; i < n; i++) {
			int g, d, s, e;
			cin >> g >> d >> s >> e;
			sale[g][0] = s; sale[g][1] = e; sale[g][2] = d;
			mx_t = max(mx_t, e);
		}

		pos P;
		for(int i = 0; i < y; i++) {
			for(int j = 0; j < x; j++) {
				if(shop[i+1][j+1] == 'P') {
					P = pos(j+1, i+1, 0);
					shop[i+1][j+1] = '.';
				}
			}
		}

		/*
		for(int i = 0; i < shop.size(); i++) {
			for(int j = 0; j < shop[i].size(); j++) {
				cout << shop[i][j];
			} cout << endl;
		}
		// */

		const int dxy[] = {0, 1, 0, -1, 0};
		int res = 0;
		queue<pos> q;
		q.push(P);
		for(int t = 0; t <= mx_t; t++) {
			queue<pos> nex;
			while(!q.empty()) {
				pos cur = q.front(); q.pop();
				
				for(int i = 0; i < 4; i++) {
					char ca = shop[cur.y+dxy[i]][cur.x+dxy[i+1]];
					if(ca != '.' && ca != '#' && sale[ca-'0'][0] <= t && t < sale[ca-'0'][1]) {
						cur.c |= 1 << (ca - '0');
					}
				}
				for(int i = 0; i < 4; i++) {
					char ca = shop[cur.y+dxy[i]][cur.x+dxy[i+1]];
					if(ca == '.' && !visited[cur.y+dxy[i]][cur.x+dxy[i+1]][t+1][cur.c]) {
						visited[cur.y+dxy[i]][cur.x+dxy[i+1]][t+1][cur.c] = true;
						nex.push(pos(cur.x + dxy[i+1], cur.y + dxy[i], cur.c));
					}
				}
			}
			if(t == mx_t) {
				while(!nex.empty()) {
					pos cur = nex.front(); nex.pop();
					int tmp = 0;
					for(int i = 0; i < 10; i++) {
						if(cur.c & (1 << i)) {
							tmp += sale[i][2];
						}
					}
					res = max(res, tmp);
				}
			}

			q = nex;
		}

		cout << res << endl;
	}
}
accuracy: 1
time_ms: 900
memory_kb: 52944
final_rank: 19

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair<int,string> P;

const static int tx[4] = {+0,+1,+0,-1};
const static int ty[4] = {-1,+0,+1,+0};

bool dp[21][21][101][1<<10];

class State{
public:
  int x;
  int y;
  int time;
  int got;
  bool operator<(const State& s) const {
    return time < s.time;
  }
  bool operator>(const State& s) const {
    return time > s.time;
  }
  State(int x,int y,int time,int got) 
    : x(x), y(y), time(time), got(got){}
};

class Goods{
public:
  int item_id;
  int discounted_price;
  int sale_start_time;
  int sale_end_time;
  Goods(int item_id,int discounted_price, int sale_start_time,int sale_end_time)
    : item_id(item_id),discounted_price(discounted_price),sale_start_time(sale_start_time), sale_end_time(sale_end_time){}
  Goods(){}
};

int main(){
  int W,H;
  while(~scanf("%d %d",&W,&H)){
    if(W == 0 && H == 0) break;
    char stage[21][21];
    memset(dp,false,sizeof(dp));
    int sx,sy;
    for(int y = 0; y < H; y++){
      for(int x = 0; x < W; x++){
        char cell[8];
        scanf("%s",cell);
        stage[y][x] = cell[0];
        if(stage[y][x] == 'P'){
          sy = y;
          sx = x;
        }
      }
    }
    int total_time_sales;
    scanf("%d",&total_time_sales);

    map<int,Goods> goods;
    for(int i = 0; i < total_time_sales; i++){
      int item_id;
      int discounted_price;
      int sale_start_time;
      int sale_end_time;
      scanf("%d %d %d %d",
            &item_id,
            &discounted_price,
            &sale_start_time,
            &sale_end_time);
      goods[item_id] = Goods(item_id,discounted_price,sale_start_time,sale_end_time);
    }
    priority_queue<State,vector<State>,greater<State> > que;
    que.push(State(sx,sy,0,0));
    dp[sx][sy][0][0] = true;
    while(!que.empty()){
      State s = que.top();
      que.pop();
      for(int i = 0; i < 4; i++){
        int dx = s.x + tx[i];
        int dy = s.y + ty[i];
        if(dx < 0 || dx >= W || dy < 0 || dy >= H) continue;
        if(stage[dy][dx] == '.' || stage[dy][dx] == 'P') continue;
        const Goods& g = goods[stage[dy][dx] - '0'];
        if(g.sale_start_time <= s.time && s.time < g.sale_end_time){
          s.got |= (1 << g.item_id);
        }
      }

      for(int i = 0; i < 4; i++){
        int dx = s.x + tx[i];
        int dy = s.y + ty[i];
        if(dx < 0 || dx >= W || dy < 0 || dy >= H) continue;
        if(stage[dy][dx] != '.' && stage[dy][dx] != 'P') continue;
        if(dp[dx][dy][s.time+1][s.got]) continue;
        if(s.time+1 > 100) continue;
        dp[dx][dy][s.time+1][s.got] = true;
        que.push(State(dx,dy,s.time+1,s.got));
      }
    }

    int res = 0;
    for(int y = 0; y < H; y++){
      for(int x = 0; x < W; x++){
        for(int time = 0; time <= 100; time++){
          for(int S = 0; S < (1<<10); S++){
            if(dp[x][y][time][S]){
              int sum = 0;
              for(int i = 0; i < 10; i++){
                if(S & (1<<i)){
                  sum += goods[i].discounted_price;
                }
              }
              res = max(res,sum);
            }
          }
        }
      }
    }
    printf("%d\n",res);
  }
}
accuracy: 1
time_ms: 840
memory_kb: 46712
final_rank: 17

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

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

struct Data {
  int cur,time,bitmask,score;
};

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int maxi[20*20][1<<8][110],info[30][30][110];
int w,h,sp,n,g[10],d[10],s[10],e[10],limit,trans[10];
char field[30][30];

bool isValid(int x,int y) { return 0 <= x && x < w && 0 <= y && y < h; }

int getDiff(int bits1,int bits2){
  int ret = 0;
  int diff = bits1 ^ bits2;
  diff = diff & bits2;
  while( diff ){
    int here = diff & -diff;
    diff -= here;
    int pos = log2(here);
    ret += d[trans[pos]];
  }
  return ret;
}

void compute(){
  rep(tm,limit) {
    rep(i,h) rep(j,w) {
      info[i][j][tm] = 0;
      rep(k,4) {
        int nx = j + dx[k], ny = i + dy[k];
        if( !isValid(nx,ny) ) continue;
        if( field[ny][nx] == '.' ) continue;
        int index = field[ny][nx] - '0';
        if( s[index] <= tm && tm < e[index] ) {
          info[i][j][tm] |= (1<<(field[ny][nx]-'0'));
        }
      }
    }
  }

  deque<Data> deq;
  rep(i,h*w) rep(j,(1<<n)) rep(k,limit) maxi[i][j][k] = -1;
  deq.push_back((Data){sp,0,0,0});
  int answer = 0;
  while( !deq.empty() ){
    Data data = deq.front(); deq.pop_front();
    if( maxi[data.cur][data.bitmask][data.time] > data.score ) continue;
    int x = data.cur % w, y = data.cur / w;
    rep(i,4){
      int nx = x + dx[i], ny = y + dy[i];
      if( !isValid(nx,ny) ) continue;
      if( field[ny][nx] != '.' ) continue;
      if( data.time + 1 >= limit ) continue;
      int ntime = data.time + 1;
      int nbitmask = data.bitmask | info[ny][nx][ntime];
      int nscore = data.score + getDiff(data.bitmask,info[ny][nx][ntime]);
      if( maxi[nx+ny*w][nbitmask][ntime] < nscore ) {
        maxi[nx+ny*w][nbitmask][ntime] = nscore;
        answer = max(answer,nscore);
        deq.push_back((Data){nx+ny*w,ntime,nbitmask,nscore});
      }
    }
  }
  cout << answer << endl;
}

int main(){
  while( cin >> w >> h, w|h ){
    rep(i,h) rep(j,w) {
      cin >> field[i][j];
      if( field[i][j] == 'P' ) {
        field[i][j] = '.';
        sp = j + i * w;
      }
    }
    limit = 0;
    cin >> n;
    rep(i,n) {
      cin >> g[i] >> d[i] >> s[i] >> e[i];
      trans[g[i]] = i;
      limit = max(limit,e[i]);
    }
    compute();
  }
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 46040
final_rank: 13

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
char in[2];
int m[30][30];
int dp[101][21][21][1<<8];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int can[11][110];
int lis[11];
int con[11];
int main(){
	int a,b;
	while(scanf("%d%d",&b,&a),a){
		int sr,sc;
		for(int i=0;i<a;i++)for(int j=0;j<b;j++){
			scanf("%s",in);
			if(in[0]=='P'){sr=i;sc=j;}
			if(in[0]=='P'||in[0]=='.')m[i][j]=-1;
			else m[i][j]=in[0]-'0';
		}
		int c;scanf("%d",&c);
		for(int i=0;i<11;i++)for(int j=0;j<110;j++)can[i][j]=0;
		for(int i=0;i<c;i++){
			int p,q,r,s;
			scanf("%d%d%d%d",&p,&q,&r,&s);
			for(int j=r;j<s;j++)can[p][j]=max(can[p][j],q);
		}
		for(int i=0;i<11;i++)lis[i]=-1;
		int sz=0;
		for(int i=0;i<10;i++){
			bool ok=false;
			for(int j=0;j<101;j++)if(can[i][j])ok=true;
			if(ok){
				con[sz]=i;
				lis[i]=sz++;
			}
		}
		for(int i=0;i<101;i++){
			for(int j=0;j<a;j++)for(int k=0;k<b;k++)for(int l=0;l<(1<<sz);l++){
				dp[i][j][k][l]=-1;
			}
		}
		int ret=0;
		dp[0][sr][sc][0]=0;
		for(int i=0;i<100;i++){
			for(int j=0;j<a;j++)for(int k=0;k<b;k++)for(int l=0;l<(1<<sz);l++){
				if(!~dp[i][j][k][l])continue;
				ret=max(ret,dp[i][j][k][l]);
				for(int x=0;x<4;x++){
					if(j+dx[x]<0||j+dx[x]>=a||k+dy[x]<0||k+dy[x]>=b)continue;
					if(~m[j+dx[x]][k+dy[x]]){
						if(!~lis[m[j+dx[x]][k+dy[x]]])continue;
						if(l&(1<<lis[m[j+dx[x]][k+dy[x]]]))continue;
						dp[i][j][k][l+(1<<lis[m[j+dx[x]][k+dy[x]]])]=max(dp[i][j][k][l+(1<<lis[m[j+dx[x]][k+dy[x]]])],dp[i][j][k][l]+can[m[j+dx[x]][k+dy[x]]][i]);
					}else{
						dp[i+1][j+dx[x]][k+dy[x]][l]=max(dp[i+1][j+dx[x]][k+dy[x]][l],dp[i][j][k][l]);
					}
				}
			}
		}
		printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 240
memory_kb: 45432
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
//とおしたいね
typedef long long int64;
//同じ商品で何回かタイムせーるしてる？
//うーん//
struct Sale{
  int g;
  int64 d;
  int s, e;
} sale[8];
struct State{
  unsigned char x, y, time;
  unsigned bit, used_pop; //used_pop: 使った商品
  State( unsigned char x, unsigned char y, unsigned bit, unsigned char time, unsigned used_pop):x(x),y(y),bit(bit),time(time),used_pop(used_pop){};
};

int W, H, N, sy, sx;
bool used[20][20][1 << 8][101];
char m[20][20];
int64 cost[1 << 8];
const int dy[] = { 0, 1, 0, -1, 0}, dx[] = { 1, 0, -1, 0, 0};
queue< State > que;
int64 BFS(){
  que.push(State( sx, sy, 0, 0, 0));
  used[sy][sx][0][0] = true;
  memset( used, false, sizeof(used));
  for(int i = 0; i < 1 << N; i++){
    cost[i] = 0;
    for(int j = 0; j < N; j++){
      if((i >> j) & 1){
        cost[i] += sale[j].d;
      }
    }
  }
  int64 ret = 0;
  while(!que.empty()){
    State p = que.front(); que.pop();
    ret = max( ret, cost[p.bit]);
    if(p.time > 100) continue;
    for(int i = 0; i < 4; i++){
      int ny = p.y + dy[i], nx = p.x + dx[i];
      if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
      if(isdigit(m[ny][nx])){
        for(int j = 0; j < N; j++){
          if(m[ny][nx] - '0' == sale[j].g && !((p.bit >> j) & 1) && !((p.used_pop >> sale[j].g) & 1)){
            if(sale[j].s <= p.time && p.time < sale[j].e){
              if(used[p.y][p.x][p.bit|(1 << j)][p.time]++) continue;
              que.push( State( p.x, p.y, p.bit|(1 << j), p.time,
                               p.used_pop|(1 << sale[j].g)));
            }
          }
        }
      }
    }
    for(int i = 0; i < 5; i++){
      int ny = p.y + dy[i], nx = p.x + dx[i];
      if(ny < 0 || ny >= H || nx < 0 || nx >= W || m[ny][nx] != '.') continue;
      if(used[ny][nx][p.bit][p.time + 1]++) continue;
      que.push( State( nx, ny, p.bit, p.time + 1, p.used_pop));
    }
  }
  return ret;
}

int main(){
  while(cin >> W >> H, W||H){
    for(int i = 0; i < H; i++){
      for(int j = 0; j < W; j++){
        cin >> m[i][j];
        if(m[i][j] == 'P') m[i][j] = '.', sy = i, sx = j;
      }
    }
    cin >> N;
    for(int i = 0; i < N; i++){
      cin >> sale[i].g >> sale[i].d >> sale[i].s >> sale[i].e;
    }
    cout << BFS() << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 12188
final_rank: 8

code_snippet: #include <cstdio>
#include <queue>
#include <cctype>
#include <cstring>
#include <cassert>

using namespace std;

struct state {
  int x,y,u,t,c;
  state(){}
  state(int x,int y,int u,int t,int c):x(x),y(y),u(u),t(t),c(c){}
};

int cost[20][20][1<<8][100];
char g[20][20],gt[20][20];
int n,v[8],d[8],s[8],e[8];
int w,h;
int dx[]={0,1,0,-1,0},dy[]={1,0,-1,0,0};

int main(void)
{
  for(;;) {
    scanf("%d%d",&w,&h); if(w+h == 0)break;
    int x,y; x = y = -1;
    for( int i = 0; i < h; i++ ) {
      for( int j = 0; j < w; j++ ) {
        scanf(" %c",g[i]+j);
        if( g[i][j] == 'P' ) {
          g[i][j] = '.';
          x = j;
          y = i;
        }
      }
    }
    memcpy(gt,g,sizeof(gt));
    for( int i = 0; i < h; i++ ) {
      for( int j = 0; j < w; j++ ) {
        if( isdigit(g[i][j]) ) {
          g[i][j] = '#';
        }
      }
    }
    assert(x>=0 && y>=0);
    scanf("%d",&n);
    int id[10],ct=0; memset(id,-1,sizeof(id));
    for( int i = 0; i < n; i++ ) {
      scanf("%d%d%d%d",v+i,d+i,s+i,e+i);
      if( id[v[i]] < 0 ) {
        id[v[i]] = ct;
        for( int j = 0; j < h; j++ ) {
          for( int k = 0; k < w; k++ ) {
            if( gt[j][k] == v[i]+'0' ) g[j][k] = ct+'0';
          }
        }
        ct += 1;
      }
      v[i] = id[v[i]];
    }
    int res = 0;
    queue<state> q;
    q.push(state(x,y,0,0,0));
    memset(cost,-1,sizeof(cost));
    while( !q.empty() ) {
      state p = q.front(); q.pop();
      //printf("%d %d %02x %d %d\n",p.x,p.y,p.u,p.t,p.c);
      if( p.t >= 100 ) continue;
      if( res < p.c ) res = p.c;
      for( int i = 0; i < 4; i++ ) {
        x = p.x+dx[i];
        y = p.y+dy[i];
        if( x < 0 || x >= w || y < 0 || y >= h ) continue;
        if( isdigit(g[y][x]) ) {
          int k = g[y][x]-'0';
          if( !(p.u>>k&1) ) {
            for( int j = 0; j < n; j++ ) {
              if( s[j] <= p.t && p.t < e[j] && v[j] == k ) {
                if( cost[p.y][p.x][p.u|1<<k][p.t] < p.c+d[j] ) {
                  cost[p.y][p.x][p.u|1<<k][p.t] = p.c+d[j];
                  q.push(state(p.x,p.y,p.u|1<<k,p.t,p.c+d[j]));
                }
              }
            }
          }
        }
        if( g[y][x] == '.' && cost[y][x][p.u][p.t+1] < p.c ) {
          cost[y][x][p.u][p.t+1] = p.c;
          q.push(state(x,y,p.u,p.t+1,p.c));
        }
      }
    }
    printf("%d\n",res);
  }
  return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 41756
final_rank: 10

code_snippet: #include <cstdio>
#include <queue>
#include <cctype>
#include <cstring>
#include <cassert>

using namespace std;

struct state {
  int x,y,u,t,c;
  state(){}
  state(int x,int y,int u,int t,int c):x(x),y(y),u(u),t(t),c(c){}
};

int cost[20][20][1<<8][100];
char g[20][20];
int n,v[8],d[8],s[8],e[8];
int w,h;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int main(void)
{
  for(;;) {
    scanf("%d%d",&w,&h); if(w+h == 0)break;
    int x,y; x = y = -1;
    for( int i = 0; i < h; i++ ) {
      for( int j = 0; j < w; j++ ) {
        scanf(" %c",g[i]+j);
        if( g[i][j] == 'P' ) {
          g[i][j] = '.';
          x = j;
          y = i;
        }
      }
    }
    assert(x>=0 && y>=0);
    scanf("%d",&n);
    for( int i = 0; i < n; i++ ) {
      scanf("%d%d%d%d",v+i,d+i,s+i,e+i);
    }
    int res = 0;
    queue<state> q;
    q.push(state(x,y,0,0,0));
    memset(cost,-1,sizeof(cost));
    while( !q.empty() ) {
      state p = q.front(); q.pop();
      //printf("%d %d %02x %d %d\n",p.x,p.y,p.u,p.t,p.c);
      if( p.t >= 100 ) continue;
      if( res < p.c ) res = p.c;
      for( int i = 0; i < 4; i++ ) {
        x = p.x+dx[i];
        y = p.y+dy[i];
        if( x < 0 || x >= w || y < 0 || y >= h ) continue;
        if( isdigit(g[y][x]) ) {
          int k = g[y][x]-'0';
          if( !(p.u>>k&1) ) {
            for( int j = 0; j < n; j++ ) {
              if( s[j] <= p.t && p.t < e[j] && v[j] == k ) {
                if( cost[p.y][p.x][p.u|1<<k][p.t] < p.c+d[j] ) {
                  cost[p.y][p.x][p.u|1<<k][p.t] = p.c+d[j];
                  q.push(state(p.x,p.y,p.u|1<<k,p.t,p.c+d[j]));
                }
              }
            }
          }
        }
        if( g[y][x] == '.' && cost[y][x][p.u][p.t+1] < p.c ) {
          cost[y][x][p.u][p.t+1] = p.c;
          q.push(state(x,y,p.u,p.t+1,p.c));
        }
      }
    }
    printf("%d\n",res);
  }
  return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 41756
final_rank: 10