Problem:
G: Quest of Merchant
ICPC で良い成績を収めるには修行が欠かせない．うさぎは ICPC で勝ちたいので，今日も修行をすることにした．
今日の修行は，街を駆け回り交易を行うことで，商売の力を手にしようというものである．
今後の修行のためにも，できるだけ多くの資金を稼ぎたい．
この世界は，東西南北方向の道路が等間隔に並んでいて，碁盤状になっている．唯一存在する市場の位置を (0, 0)，街には
x
座標，
y
座標が定まっている (座標が整数値の点が交差点に対応する)．うさぎは道路に沿ってのみ移動することができ，隣接する交差点間を移動するのに 1 分を要する．いくつかの交差点には街がある．交易では，街で商品を買い，市場にて販売することによって価格の差の分の利益を得る．
うさぎの初期資金は十分にあり，お金が足りないから商品が購入できない，といったことはない．だが，それぞれの商品には重さがあり，うさぎは重さの合計が
W
までの商品しか同時に持ち運ぶことができない．よって，街に商品を仕入れに行ったり市場に戻ったりを繰り返すことになる．場合によっては，複数の街で商品を購入してから市場に戻ることになるかもしれない．
街での商品の購入や，市場での商品の販売は一瞬で行えるものとする．また，街にある商品が品切れになってしまうことは有り得ず，無限に購入することが可能である．
うさぎは，今回売買を行おうとしている各商品の名前，1 個あたりの重さおよび販売価格と，各街の
x
座標，
y
座標および販売商品の名前と価格を，データにまとめた．うさぎは今，市場がある (0, 0) の位置にいる．プログラムを使って，制限時間
T
分の間にどれだけ稼ぐことが可能か調べたい．
Input
N
M
W
T
S
1
V
1
P
1
...
S
M
V
M
P
M
L
1
X
1
Y
1
R
1,1
Q
1,1
...
R
1,
L
1
Q
1,
L
1
...
L
N
X
N
Y
N
R
N
,1
Q
N
,1
...
R
N
,
L
N
Q
N
,
L
N
N
は街の数，
M
は商品の種類数である．
S
i
,
V
i
,
P
i
(1 ≤
i
≤
M
) はそれぞれ，
i
番目の商品の名前，1 個あたりの重さ，1 個あたりの販売価格である．
街は 1 以上
N
以下の整数で表される．
L
j
,
X
j
,
Y
j
(1 ≤
j
≤
N
) はそれぞれ，街
j
で売られている商品の種類数，街
j
の
x
座標，
y
座標である．
R
j
,
k
,
Q
j
,
k
(1 ≤
j
≤
N
，1 ≤
k
≤
L
j
) はそれぞれ，街
j
で売られている
k
番目の商品の名前，価格である．
1 ≤
N
≤ 7，1 ≤
M
≤ 7，1 ≤
W
≤ 10,000，1 ≤
T
≤ 10,000，1 ≤
V
i
≤
W
，1 ≤
P
i
≤ 10,000，1 ≤
L
j
≤
M
，-10,000 ≤
X
j
≤ 10,000，-10,000 ≤
Y
j
≤ 10,000，1 ≤
Q
j
,
k
≤ 10,000 を満たす．商品の名前はアルファベット小文字からなる長さが 1 以上 7 以下の文字列である．その他の値はすべて整数である．
S
i
はすべて異なる．(
X
j
,
Y
j
) として同一の組は複数回現れず，(
X
j
,
Y
j
) = (0, 0) となることはない．
各
j
に対して
R
j
,
k
はすべて異なり，各
R
j
，
k
はいずれかの
S
i
に一致する．
Output
うさぎが得られる最大の利益を 1 行に出力せよ．
Sample Input 1
2 2 100 20
alfalfa 10 10
carrot 5 10
1 1 6
carrot 4
1 -3 0
alfalfa 5
Sample Output 1
170
Sample Input 2
2 3 100 20
vim 10 10
emacs 5 10
vstudio 65 100
2 1 6
emacs 4
vstudio 13
3 -3 0
vim 5
emacs 9
vstudio 62
Sample Output 2
183

Submitted solutions:

code_snippet: #include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
int N, M, W, T, V[7], P[7], L[7], X[7], Y[7], R[7][7], Q[7][7];
std::string S[7];
int posX(int i) {
    assert(0 <= i and i <= N);
    return i < N ? X[i] : 0;
}
int posY(int i) {
    assert(0 <= i and i <= N);
    return i < N ? Y[i] : 0;
}
int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cin >> N >> M >> W >> T;
    for (int i = 0 ; i < M ; i++) std::cin >> S[i] >> V[i] >> P[i];
    for (int i = 0 ; i < N ; i++) {
        std::cin >> L[i] >> X[i] >> Y[i];
        for (int j = 0 ; j < L[i] ; j++) {
            std::string name;
            std::cin >> name >> Q[i][j];
            R[i][j] = -1;
            for (int k = 0 ; k < M ; k++) if (name == S[k]) {
                assert(R[i][j] == -1);
                R[i][j] = k;
            }
            assert(R[i][j] != -1);
        }
    }
    // 街の集合に対して、最短で巡回するのにかかる時間
    std::vector<int> time = [&]() -> std::vector<int> {
        const int INF = (int)2e9;
        std::vector dp(N + 1, std::vector<int>(1 << (N + 1), INF));
        dp[N][1 << N] = 0;
        for (int b = (1 << N) ; b < (1 << (N + 1)) ; b++) {
            for (int u = 0 ; u < N + 1 ; u++) if ((b & (1 << u)) and dp[u][b] != INF) {
                for (int v = 0 ; v < N ; v++) if (!(b & (1 << v))) {
                    int w = std::abs(posX(u) - posX(v)) + std::abs(posY(u) - posY(v));
                    assert(w > 0);
                    dp[v][b | (1 << v)] = std::min(dp[v][b | (1 << v)], dp[u][b] + w);
                }
            }
        }
        std::vector<int> res(1 << N, INF);
        for (int b = (1 << N) ; b < (1 << (N + 1)) ; b++) {
            assert(b & (1 << N));
            int nb = b ^ (1 << N);
            for (int i = 0 ; i < N ; i++) if (b & (1 << i)) {
                assert(dp[i][b] != INF);
                int w = std::abs(posX(i)) + std::abs(posY(i));
                assert(w > 0);
                res[nb] = std::min(res[nb], dp[i][b] + w);
            }
        }
        return res;
    }();
    // 集合を巡回したときの得られる利得
    std::vector<long long> kiyo = [&]() -> std::vector<long long> {
        std::vector<long long> res(1 << N);
        for (int b = 0 ; b < (1 << N) ; b++) {
            std::vector<int> val(M, -1);
            for (int i = 0 ; i < N ; i++) if (b & (1 << i)) {
                for (int j = 0 ; j < L[i] ; j++) {
                    int id = R[i][j];
                    val[id] = std::max(val[id], P[id] - Q[i][j]);
                }
            }
            std::vector<long long> dp(W + 1, -1);
            dp[0] = 0;
            for (int i = 0 ; i < M ; i++) if (val[i] >= 1) {
                std::vector<long long> next = dp;
                for (int j = V[i] ; j <= W ; j++) if (next[j - V[i]] >= 0) {
                    next[j] = std::max(next[j], next[j - V[i]] + val[i]);
                }
                dp = std::move(next);
            }
            res[b] = *std::max_element(dp.begin(), dp.end());
            assert(res[b] >= 0);
        }
        return res;
    }();
    // for (int i = 0 ; i < (1 << N) ; i++) std::cout << '(' << time[i] << ',' << kiyo[i] << ')';
    // std::cout << std::endl;
    std::vector<long long> dp(T + 1, -1LL);
    dp[0] = 0;
    for (int b = 0 ; b < (1 << N) ; b++) if (kiyo[b] >= 1) {
        std::vector<long long> next = dp;
        for (int i = time[b] ; i <= T ; i++) if (next[i - time[b]] >= 0) {
            next[i] = std::max(next[i], next[i - time[b]] + kiyo[b]);
        }
        dp = std::move(next);
    }
    std::cout << *std::max_element(dp.begin(), dp.end()) << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 3840
final_rank: 7

code_snippet: // AOJ #2296
// Quest of Merchant 2025.2.10

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

const int INF_INT = 1e9;
const ll NEG_INF_LL = -1e18;
 
int manhattan(int x1, int y1, int x2, int y2){
    return abs(x1-x2) + abs(y1-y2);
}
 
struct Product {
    string name;
    int weight;
    int marketPrice;
};
 
struct TownOffer {
    int prodIndex;
    int price;
};
 
struct Town {
    int x, y;
    vector<TownOffer> offers;
};
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int N, M, W, T;
    cin >> N >> M >> W >> T;
    vector<Product> products(M);
    unordered_map<string,int> prodIndexMap;
    for (int i = 0; i < M; i++){
        cin >> products[i].name >> products[i].weight >> products[i].marketPrice;
        prodIndexMap[products[i].name] = i;
    }
 
    vector<Town> towns(N);
    for (int j = 0; j < N; j++){
        int L, X, Y;
        cin >> L >> X >> Y;
        towns[j].x = X;
        towns[j].y = Y;
        towns[j].offers.resize(L);
        for (int k = 0; k < L; k++){
            string prodName;
            int price;
            cin >> prodName >> price;
            int pindex = prodIndexMap[prodName];
            towns[j].offers[k] = {pindex, price};
        }
    }
 
    vector<int> distMarket(N);
    for (int i = 0; i < N; i++){
        distMarket[i] = manhattan(0, 0, towns[i].x, towns[i].y);
    }
    vector<vector<int>> distTown(N, vector<int>(N, 0));
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            distTown[i][j] = manhattan(towns[i].x, towns[i].y, towns[j].x, towns[j].y);
        }
    }
 
    int totSub = 1 << N;
    vector<vector<int>> dp(totSub, vector<int>(N, INF_INT));
    for (int j = 0; j < N; j++){
        int mask = 1 << j;
        dp[mask][j] = distMarket[j];
    }
    for (int mask = 0; mask < totSub; mask++){
        for (int j = 0; j < N; j++){
            if(!(mask & (1 << j))) continue;
            for (int k = 0; k < N; k++){
                if(mask & (1 << k)) continue;
                int nextMask = mask | (1 << k);
                dp[nextMask][k] = min(dp[nextMask][k], dp[mask][j] + distTown[j][k]);
            }
        }
    }
    vector<int> cycleTime(totSub, INF_INT); 
    for (int mask = 1; mask < totSub; mask++){
        int best = INF_INT;
        for (int j = 0; j < N; j++){
            if(mask & (1 << j))
                best = min(best, dp[mask][j] + distMarket[j]);
        }
        cycleTime[mask] = best;
    }
 
    vector<pair<int, ll>> cycles;
    for (int mask = 1; mask < totSub; mask++){
        const int INF_PRICE = 1000000000;
        vector<int> bestPrice(M, INF_PRICE);
        for (int j = 0; j < N; j++){
            if(mask & (1 << j)){
                for(auto &offer : towns[j].offers){
                    int pidx = offer.prodIndex;
                    bestPrice[pidx] = min(bestPrice[pidx], offer.price);
                }
            }
        }
        vector<pair<int,int>> items;
        for (int i = 0; i < M; i++){
            if(bestPrice[i] < INF_PRICE){
                int prodProfit = products[i].marketPrice - bestPrice[i];
                if(prodProfit > 0)
                    items.push_back({products[i].weight, prodProfit});
            }
        }
        if(items.size() == 0) continue;
 
        vector<ll> knap(W+1, NEG_INF_LL);
        knap[0] = 0;
        for (int w = 0; w <= W; w++){
            if(knap[w] == NEG_INF_LL) continue;
            for(auto &it : items){
                int wt = it.first, prof = it.second;
                if(w + wt <= W) knap[w+wt] = max(knap[w+wt], knap[w] + prof);
            }
        }
        ll bestCycleProfit = 0;
        for (int w = 0; w <= W; w++)
            bestCycleProfit = max(bestCycleProfit, knap[w]);
        if(bestCycleProfit > 0){
            int tCycle = cycleTime[mask];
            cycles.push_back({tCycle, bestCycleProfit});
        }
    }
 
    vector<ll> dpTime(T+1, NEG_INF_LL);
    dpTime[0] = 0;
    for (int t = 0; t <= T; t++){
        if(dpTime[t] == NEG_INF_LL) continue;
        for(auto &cycle : cycles){
            int ct = cycle.first;
            ll cp = cycle.second;
            if(t + ct <= T) dpTime[t+ct] = max(dpTime[t+ct], dpTime[t] + cp);
        }
    }
    ll ans = 0;
    for (int t = 0; t <= T; t++) ans = max(ans, dpTime[t]);
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3640
final_rank: 4

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    ll N, M, W, T;
    cin >> N >> M >> W >> T;
    map<string,int> mps;
    vector<ll> V(M), P(M);
    rep(i,0,M) {
        string _;
        cin >> _;
        mps[_] = i;
        cin >> V[i] >> P[i];
    }
    vector<ll> X(N), Y(N);
    vector<vector<ll>> R(N), Q(N);
    rep(i,0,N) {
        int L;
        cin >> L;
        cin >> X[i] >> Y[i];
        R[i].resize(L);
        Q[i].resize(L);
        rep(j,0,L) {
            string s;
            cin >> s;
            R[i][j] = mps[s];
            cin >> Q[i][j];
        }
    }
    auto dist = [&](int i, int j) -> ll {return abs(X[i]-X[j])+abs(Y[i]-Y[j]);};
    vector<ll> Len(1<<N,INF);
    {
        vector<vector<ll>> DP(1<<N,vector<ll>(N,INF));
        rep(i,0,N) DP[1<<i][i] = abs(X[i]) + abs(Y[i]);
        rep(i,1,1<<N) {
            rep(j,0,N) {
                if (DP[i][j] == INF) continue;
                rep(k,0,N) {
                    if (i & (1<<k)) continue;
                    chmin(DP[i|(1<<k)][k], DP[i][j]+dist(j,k));
                }
            }
        }
        rep(i,1,1<<N) {
            rep(j,0,N) {
                if (i&(1<<j)) {
                    chmin(Len[i], DP[i][j]+abs(X[j])+abs(Y[j]));
                }
            }
        }
    }
    vector<ll> Price(1<<N,INF);
    rep(i,1,1<<N) {
        vector<ll> Cost(M,0);
        rep(j,0,N) {
            if (i&(1<<j)) {
                rep(k,0,R[j].size()) {
                    chmax(Cost[R[j][k]], P[R[j][k]]-Q[j][k]);
                }
            }
        }
        vector<ll> DP(W+1,0);
        rep(j,0,M) {
            rep(k,0,W+1) {
                if (k+V[j] <= W) chmax(DP[k+V[j]], DP[k]+Cost[j]);
            }
        }
        Price[i] = DP[W];
    }
    {
        vector<ll> DP(T+1,0);
        rep(i,1,1<<N) {
            rep(j,0,T+1) {
                if (j+(int)Len[i]<=T) chmax(DP[j+Len[i]], DP[j]+Price[i]);
            }
        }
        cout << DP[T] << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3532
final_rank: 1

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

i64 knapsack(const vector<pair<int,int>>& wv, int W) {
    const int n = wv.size();
    vector dp(n + 1, vector(W + 1, i64(0)));
    for(int i : rep(n)) {
        auto [w, v] = wv[i];
        for(int s = 0; s <= W; s++) {
            chmax(dp[i + 1][s], dp[i][s]);
            if(0 <= s - w) chmax(dp[i + 1][s], dp[i + 1][s - w] + v);
        }
    }
    return dp[n][W];
}

int main() {
    int N = in(), M = in(), W = in(), T = in();
    map<string, pair<int,int>> item;
    for(int i : rep(M)) {
        string s = in(); int w = in(), v = in();
        item[s] = {w, v};
    }

    vector<pair<int,int>> pos(N);
    vector<vector<pair<int,int>>> city(N);
    for(int i : rep(N)) {
        int L = in(), x = in(), y = in();
        pos[i] = {x, y};
        for(int j : rep(L)) {
            string s = in(); int p = in();
            auto [w, v] = item[s];
            city[i].push_back({w, v - p});
        }
    }

    vector<pair<int,int>> b;
    for(int S : rep(1 << N)) if(S != 0) {
        vector<int> I;
        for(int i : rep(N)) if(S & (1 << i)) I.push_back(i);
        const int len = I.size();
        int TS = 1e9;
        do {
            int cur = 0;
            cur += abs(pos[I[0]].first - 0) + abs(pos[I[0]].second - 0);
            for(int i : rep(len - 1)) 
                cur += abs(pos[I[i]].first - pos[I[i + 1]].first) + abs(pos[I[i]].second - pos[I[i + 1]].second);
            cur += abs(pos[I[len - 1]].first - 0) + abs(pos[I[len - 1]].second - 0);
            chmin(TS, cur);
        } while(next_permutation(I.begin(), I.end()));

        vector<pair<int,int>> a;
        for(int i : I) a.insert(a.end(), city[i].begin(), city[i].end());
        int VS = knapsack(a, W);
        b.push_back({TS, VS});
    }

    print(knapsack(b, T));
}
accuracy: 1
time_ms: 110
memory_kb: 13244
final_rank: 13

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int knapsack(const vector<pair<int,int>>& wv, int W) {
    const int n = wv.size();
    vector dp(n + 1, vector(W + 1, 0));
    for(int i : rep(n)) {
        auto [w, v] = wv[i];
        for(int s = 0; s <= W; s++) {
            chmax(dp[i + 1][s], dp[i][s]);
            if(0 <= s - w) chmax(dp[i + 1][s], dp[i + 1][s - w] + v);
        }
    }
    return dp[n][W];
}

int main() {
    int N = in(), M = in(), W = in(), T = in();
    map<string, pair<int,int>> item;
    for(int i : rep(M)) {
        string s = in(); int w = in(), v = in();
        item[s] = {w, v};
    }

    vector<pair<int,int>> pos(N);
    vector<vector<pair<int,int>>> city(N);
    for(int i : rep(N)) {
        int L = in(), x = in(), y = in();
        pos[i] = {x, y};
        for(int j : rep(L)) {
            string s = in(); int p = in();
            auto [w, v] = item[s];
            city[i].push_back({w, v - p});
        }
    }

    vector<pair<int,int>> b;
    for(int S : rep(1 << N)) if(S != 0) {
        vector<int> I;
        for(int i : rep(N)) if(S & (1 << i)) I.push_back(i);
        const int len = I.size();
        int TS = 1e9;
        do {
            int cur = 0;
            cur += abs(pos[I[0]].first - 0) + abs(pos[I[0]].second - 0);
            for(int i : rep(len - 1)) 
                cur += abs(pos[I[i]].first - pos[I[i + 1]].first) + abs(pos[I[i]].second - pos[I[i + 1]].second);
            cur += abs(pos[I[len - 1]].first - 0) + abs(pos[I[len - 1]].second - 0);
            chmin(TS, cur);
        } while(next_permutation(I.begin(), I.end()));

        vector<pair<int,int>> a;
        for(int i : I) a.insert(a.end(), city[i].begin(), city[i].end());
        int VS = knapsack(a, W);
        b.push_back({TS, VS});
    }

    print(knapsack(b, T));
}
accuracy: 0.08333333333333333
time_ms: 60
memory_kb: 8076
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	solve();
}

void solve(){
	int N,M,W,T;
	cin>>N>>M>>W>>T;
	map<string,pair<int,int>> m;
	auto dp=[](int lim,vector<pair<int,ll>> p)->ll{
		vector<ll> ans(lim+1);
		for(auto x:p){
			int ind=0;
			while(ind+x.first<=lim){
				chmax(ans[ind+x.first],ans[ind]+x.second);
				ind++;
			}
		}
		return ans[lim];
	};
	vector<int> X(N+1),Y(N+1);
	vector<vector<int>> dist(N+1,vector<int>(N+1));
	rep(i,M){
		string S;
		int v,p;
		cin>>S>>v>>p;
		m[S]={v,p};
	}
	vector<vector<pair<int,int>>> buy(N);
	rep(i,N){
		int L;
		cin>>L>>X[i]>>Y[i];
		rep(j,L){
			string S;int p;
			cin>>S>>p;
			buy[i].push_back({m[S].first,m[S].second-p});
		}
	}
	vector<pair<int,ll>> runner;
	rep(i,N+1) rep(j,N+1) dist[i][j]=(abs(X[i]-X[j])+abs(Y[i]-Y[j]));
	rep(i,(1<<N)){
		if(i==0) continue;
		vector<pair<int,ll>> cost;
		vector<int> ind;
		int K=0;
		rep(j,N){
			if(i&(1<<j)){
				for(auto x:buy[j]) cost.push_back(x);
				ind.push_back(j);K++;
			}
		}
		vector<vector<int>> sale(K,vector<int>(1<<K,INF));
		rep(j,K) sale[j][(1<<j)]=dist[ind[j]][N];
		rep(k,(1<<K)) rep(j,K){
			if(sale[j][k]==INF) continue;
			rep(l,K){
				if(((1<<l)&k)==0){
					chmin(sale[l][k+(1<<l)],sale[j][k]+dist[ind[j]][ind[l]]);
				}
			}
		}
		int t=INF;
		rep(j,K) chmin(t,sale[j][(1<<K)-1]+dist[ind[j]][N]);
		runner.push_back({t,dp(W,cost)});
	}
	//for(auto x:runner) cout<<x.first<<" "<<x.second<<"\n";
	cout<<dp(T,runner)<<"\n";
}
accuracy: 1
time_ms: 20
memory_kb: 3552
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	solve();
}

void solve(){
	int N,M,W,T;
	cin>>N>>M>>W>>T;
	map<string,pair<int,int>> m;
	auto dp=[](int lim,vector<pair<int,int>> p)->int{
		vector<int> ans(lim+1);
		for(auto x:p){
			int ind=0;
			while(ind+x.first<=lim){
				chmax(ans[ind+x.first],ans[ind]+x.second);
				ind++;
			}
		}
		return ans[lim];
	};
	vector<int> X(N+1),Y(N+1);
	vector<vector<int>> dist(N+1,vector<int>(N+1));
	rep(i,M){
		string S;
		int v,p;
		cin>>S>>v>>p;
		m[S]={v,p};
	}
	vector<vector<pair<int,int>>> buy(N);
	rep(i,N){
		int L;
		cin>>L>>X[i]>>Y[i];
		rep(j,L){
			string S;int p;
			cin>>S>>p;
			buy[i].push_back({m[S].first,m[S].second-p});
		}
	}
	vector<pair<int,int>> runner;
	rep(i,N+1) rep(j,N+1) dist[i][j]=(abs(X[i]-X[j])+abs(Y[i]-Y[j]));
	rep(i,(1<<N)){
		if(i==0) continue;
		vector<pair<int,int>> cost;
		vector<int> ind;
		int K=0;
		rep(j,N){
			if(i&(1<<j)){
				for(auto x:buy[j]) cost.push_back(x);
				ind.push_back(j);K++;
			}
		}
		vector<vector<int>> sale(K,vector<int>(1<<K,INF));
		rep(j,K) sale[j][(1<<j)]=dist[ind[j]][N];
		rep(k,(1<<K)) rep(j,K){
			if(sale[j][k]==INF) continue;
			rep(l,K){
				if(((1<<l)&k)==0){
					chmin(sale[l][k+(1<<l)],sale[j][k]+dist[ind[j]][ind[l]]);
				}
			}
		}
		int t=INF;
		rep(j,K) chmin(t,sale[j][(1<<K)-1]+dist[ind[j]][N]);
		runner.push_back({t,dp(W,cost)});
	}
	//for(auto x:runner) cout<<x.first<<" "<<x.second<<"\n";
	cout<<dp(T,runner)<<"\n";
}
accuracy: 0.08333333333333333
time_ms: 20
memory_kb: 3400
final_rank: 16

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1.0);
#line 6 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os<<'('<<p.first<< ','<<p.second<<')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?"":" ")<<*it; } return os;
}
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cout<<head<<'\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));
}
void sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }
void setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }
void Yes(bool is_correct) { std::cout<<(is_correct?"Yes":"No")<<std::endl; }
void YES(bool is_correct) { std::cout<<(is_correct?"YES":"NO")<<std::endl; }
#line 3 "a.cpp"

int main(void) {
#define int ll
    sonic();
    int n, m, w, t;
    cin >> n >> m >> w >> t;
    unordered_map<string, pair<int, int>> mp;
    rep(i, m) {
        string s;
        int v, p;
        cin >> s >> v >> p;
        mp[s] = {v, p};
    }
    vector<int> l(n), x(n), y(n);
    vector<vector<int>> v(n), q(n);
    rep(i, n) {
        cin >> l[i] >> x[i] >> y[i];
        v[i].resize(l[i]);
        q[i].resize(l[i]);
        rep(j, l[i]) {
            string s;
            cin >> s >> q[i][j];
            v[i][j] = mp[s].first;
            q[i][j] = mp[s].second - q[i][j];
        }
    }

    vector<int> idx(n);
    iota(all(idx), 0);

    vector<int> tm(1 << n, Inf);
    do {
        int s = 0;
        int xx = 0, yy = 0;
        int b = 0;
        for (auto i : idx) {
            b |= 1 << i;
            s += abs(xx - x[i]) + abs(yy - y[i]);
            xx = x[i], yy = y[i];
            chmin(tm[b], s + abs(xx) + abs(yy));
        }
    } while (next_permutation(all(idx)));

    vector<int> ans(t + 1);
    rep(bit, 1 << n) {
        vector<int> dp(w + 1);
        rep(i, n) {
            if (!(bit >> i & 1))
                continue;
            rep(j, l[i]) {
                rep(k, w - v[i][j] + 1) {
                    chmax(dp[k + v[i][j]], dp[k] + q[i][j]);
                }
            }
        }
        if (tm[bit] <= t)
            chmax(ans[tm[bit]], *max_element(all(dp)));
    }

    repn(i, t) {
        rep(j, t + 1 - i) chmax(ans[j + i], ans[j] + ans[i]);
    }
    co(*max_element(all(ans)));

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3600
final_rank: 10

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1.0);
#line 6 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os<<'('<<p.first<< ','<<p.second<<')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?"":" ")<<*it; } return os;
}
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cout<<head<<'\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));
}
void sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }
void setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }
void Yes(bool is_correct) { std::cout<<(is_correct?"Yes":"No")<<std::endl; }
void YES(bool is_correct) { std::cout<<(is_correct?"YES":"NO")<<std::endl; }
#line 3 "a.cpp"

int main(void) {
    sonic();
    int n, m, w, t;
    cin >> n >> m >> w >> t;
    unordered_map<string, pair<int, int>> mp;
    rep(i, m) {
        string s;
        int v, p;
        cin >> s >> v >> p;
        mp[s] = {v, p};
    }
    vector<int> l(n), x(n), y(n);
    vector<vector<int>> v(n), q(n);
    rep(i, n) {
        cin >> l[i] >> x[i] >> y[i];
        v[i].resize(l[i]);
        q[i].resize(l[i]);
        rep(j, l[i]) {
            string s;
            cin >> s >> q[i][j];
            v[i][j] = mp[s].first;
            q[i][j] = mp[s].second - q[i][j];
        }
    }

    vector<int> idx(n);
    iota(all(idx), 0);

    vector<int> tm(1 << n, Inf);
    do {
        int s = 0;
        int xx = 0, yy = 0;
        int b = 0;
        for (auto i : idx) {
            b |= 1 << i;
            s += abs(xx - x[i]) + abs(yy - y[i]);
            xx = x[i], yy = y[i];
            chmin(tm[b], s + abs(xx) + abs(yy));
        }
    } while (next_permutation(all(idx)));

    vector<int> ans(t + 1);
    rep(bit, 1 << n) {
        vector<int> dp(w + 1);
        rep(i, n) {
            if (!(bit >> i & 1))
                continue;
            rep(j, l[i]) {
                rep(k, w - v[i][j] + 1) {
                    chmax(dp[k + v[i][j]], dp[k] + q[i][j]);
                }
            }
        }
        if (tm[bit] <= t)
            chmax(ans[tm[bit]], *max_element(all(dp)));
    }

    repn(i, t) {
        rep(j, t + 1 - i) chmax(ans[j + i], ans[j] + ans[i]);
    }
    co(*max_element(all(ans)));

    return 0;
}
accuracy: 0.08333333333333333
time_ms: 50
memory_kb: 3444
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n, m, w, t;
    cin >> n >> m >> w >> t;
    map<string, pair<ll, ll>> products;
    rep(i,0,m) {
        string s;
        int v, p;
        cin >> s >> v >> p;
        products[s] = {v, p};
    }
    vector<ll> x(n), y(n);
    vector<map<string, ll>> sell(n);
    rep(i,0,n) {
        int l;
        cin >> l >> x[i] >> y[i];
        rep(j,0,l) {
            string r;
            int q;
            cin >> r >> q;
            sell[i][r] =q;
        }
    }

    vector<ll> cost(1<<n, 1e9), prof(1<<n);

    rep(S,1,1<<n) {
        vector<int> visit;
        rep(i,0,n) {
            if (S>>i&1) visit.push_back(i);
        }
        int k = __builtin_popcount(S);
        do {
            ll d = 0;
            d += abs(x[visit[0]]) + abs(y[visit[0]]);
            rep(i,0,k-1) {
                d += abs(x[visit[i+1]]-x[visit[i]]) + abs(y[visit[i+1]]-y[visit[i]]);
            }
            d += abs(x[visit[k-1]]) + abs(y[visit[k-1]]);
            cost[S] = min(cost[S], d);
        } while (next_permutation(visit.begin(), visit.end()));

        map<string, ll> min_price;
        rep(i,0,n) {
            if (!(S>>i&1)) continue;
            for (auto [r, q] : sell[i]) {
                if (!min_price.count(r)) min_price[r] = q;
                else min_price[r] = min(min_price[r], q);
            }
        }

        vector<ll> dp(w+1);
        for (auto [s, p] : min_price) {
            auto [weight, gain] = products[s];
            gain -= p;
            if (gain <= 0) continue;
            rep(j,0,w-weight+1) {
                dp[j+weight] = max(dp[j+weight], dp[j]+gain);
            }
        }

        prof[S] = dp[w];
    }

    vector<ll> dp(t+1);
    rep(i,0,1<<n) {
        rep(j,0,t-cost[i]+1) {
            dp[j+cost[i]] = max(dp[j+cost[i]], dp[j]+prof[i]);
        }
    }
    cout << dp[t] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3564
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n, m, w, t;
    cin >> n >> m >> w >> t;
    map<string, pair<int, int>> products;
    rep(i,0,m) {
        string s;
        int v, p;
        cin >> s >> v >> p;
        products[s] = {v, p};
    }
    vector<int> x(n), y(n);
    vector<map<string, int>> sell(n);
    rep(i,0,n) {
        int l;
        cin >> l >> x[i] >> y[i];
        rep(j,0,l) {
            string r;
            int q;
            cin >> r >> q;
            sell[i][r] =q;
        }
    }

    vector<int> cost(1<<n, 1e9), prof(1<<n);

    rep(S,1,1<<n) {
        vector<int> visit;
        rep(i,0,n) {
            if (S>>i&1) visit.push_back(i);
        }
        int k = __builtin_popcount(S);
        do {
            int d = 0;
            d += abs(x[visit[0]]) + abs(y[visit[0]]);
            rep(i,0,k-1) {
                d += abs(x[visit[i+1]]-x[visit[i]]) + abs(y[visit[i+1]]-y[visit[i]]);
            }
            d += abs(x[visit[k-1]]) + abs(y[visit[k-1]]);
            cost[S] = min(cost[S], d);
        } while (next_permutation(visit.begin(), visit.end()));

        map<string, int> min_price;
        rep(i,0,n) {
            if (!(S>>i&1)) continue;
            for (auto [r, q] : sell[i]) {
                if (!min_price.count(r)) min_price[r] = q;
                else min_price[r] = min(min_price[r], q);
            }
        }

        vector<int> dp(w+1);
        for (auto [s, p] : min_price) {
            auto [weight, gain] = products[s];
            gain -= p;
            if (gain <= 0) continue;
            rep(j,0,w-weight+1) {
                dp[j+weight] = max(dp[j+weight], dp[j]+gain);
            }
        }

        prof[S] = dp[w];
    }

    vector<int> dp(t+1);
    rep(i,0,1<<n) {
        rep(j,0,t-cost[i]+1) {
            dp[j+cost[i]] = max(dp[j+cost[i]], dp[j]+prof[i]);
        }
    }
    cout << dp[t] << endl;
}
accuracy: 0.08333333333333333
time_ms: 10
memory_kb: 3500
final_rank: 15

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

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

const ll MOD = 1e9 + 7;

// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
    ll a = 1, p = x;
    while (y > 0) {
        if (y % 2 == 0) {
            p = (p * p) % m;
            y /= 2;
        } else {
            a = (a * p) % m;
            y--;
        }
    }
    return a;
}
// END CUT

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

string S[7];
int V[7], P[7], L[7], X[7], Y[7], R[7][7], Q[7][7];
ll ma[1 << 7], tim[1 << 7];
ll dp[50][10001];
ll dp2[1 << 7][10001];

int main() {
    int N, M, W, T;
    cin >> N >> M >> W >> T;
    map<string, int> idx;
    for (int i = 0; i < M; i++) {
        cin >> S[i] >> V[i] >> P[i];
        idx[S[i]] = i;
    }
    for (int i = 0; i < N; i++) {
        cin >> L[i] >> X[i] >> Y[i];
        for (int j = 0; j < L[i]; j++) {
            string s;
            cin >> s >> Q[i][j];
            R[i][j] = idx[s];
        }
    }
    for (int bit = 0; bit < (1 << N); bit++) {
        vector<pair<int, int>> prod;
        for (int i = 0; i < N; i++) {
            if ((bit & (1 << i)) == 0)
                continue;
            for (int j = 0; j < L[i]; j++) {
                prod.emplace_back(V[R[i][j]], P[R[i][j]] - Q[i][j]);
            }
        }
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < prod.size(); i++) {
            auto [v, p] = prod[i];
            for (int j = 0; j <= W; j++) {
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
                if (j >= v)
                    dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - v] + p);
            }
        }
        for (int i = 0; i <= W; i++)
            ma[bit] = max(ma[bit], dp[prod.size()][i]);
    }
    for (int bit = 0; bit < (1 << N); bit++) {
        vector<int> perm;
        for (int i = 0; i < N; i++) {
            if (bit & (1 << i))
                perm.push_back(i);
        }
        int mi = 1 << 30;
        do {
            int prevX = 0, prevY = 0;
            int sum = 0;
            for (int p : perm) {
                sum += abs(X[p] - prevX) + abs(Y[p] - prevY);
                prevX = X[p];
                prevY = Y[p];
            }
            sum += abs(prevX) + abs(prevY);
            mi = min(mi, sum);
        } while (next_permutation(ALL(perm)));
        tim[bit] = mi;
    }
    for (int i = 1; i < (1 << N); i++) {
        for (int j = 0; j <= T; j++) {
            dp2[i][j] = max(dp2[i][j], dp2[i - 1][j]);
            if (j >= tim[i])
                dp2[i][j] = max(dp2[i][j], dp2[i][j - tim[i]] + ma[i]);
        }
    }
    ll ans = 0;
    for (int i = 0; i <= T; i++)
        ans = max(ans, dp2[(1 << N) - 1][i]);
    cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 17344
final_rank: 12

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

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

const ll MOD = 1e9 + 7;

// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
    ll a = 1, p = x;
    while (y > 0) {
        if (y % 2 == 0) {
            p = (p * p) % m;
            y /= 2;
        } else {
            a = (a * p) % m;
            y--;
        }
    }
    return a;
}
// END CUT

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

string S[7];
int V[7], P[7], L[7], X[7], Y[7], R[7][7], Q[7][7];
int ma[1 << 7], tim[1 << 7];
int dp[50][10001];
int dp2[1 << 7][10001];

int main() {
    int N, M, W, T;
    cin >> N >> M >> W >> T;
    map<string, int> idx;
    for (int i = 0; i < M; i++) {
        cin >> S[i] >> V[i] >> P[i];
        idx[S[i]] = i;
    }
    for (int i = 0; i < N; i++) {
        cin >> L[i] >> X[i] >> Y[i];
        for (int j = 0; j < L[i]; j++) {
            string s;
            cin >> s >> Q[i][j];
            R[i][j] = idx[s];
        }
    }
    for (int bit = 0; bit < (1 << N); bit++) {
        vector<pair<int, int>> prod;
        for (int i = 0; i < N; i++) {
            if ((bit & (1 << i)) == 0)
                continue;
            for (int j = 0; j < L[i]; j++) {
                prod.emplace_back(V[R[i][j]], P[R[i][j]] - Q[i][j]);
            }
        }
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < prod.size(); i++) {
            auto [v, p] = prod[i];
            for (int j = 0; j <= W; j++) {
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
                if (j >= v)
                    dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - v] + p);
            }
        }
        for (int i = 0; i <= W; i++)
            ma[bit] = max(ma[bit], dp[prod.size()][i]);
    }
    for (int bit = 0; bit < (1 << N); bit++) {
        vector<int> perm;
        for (int i = 0; i < N; i++) {
            if (bit & (1 << i))
                perm.push_back(i);
        }
        int mi = 1 << 30;
        do {
            int prevX = 0, prevY = 0;
            int sum = 0;
            for (int p : perm) {
                sum += abs(X[p] - prevX) + abs(Y[p] - prevY);
                prevX = X[p];
                prevY = Y[p];
            }
            sum += abs(prevX) + abs(prevY);
            mi = min(mi, sum);
        } while (next_permutation(ALL(perm)));
        tim[bit] = mi;
    }
    for (int i = 1; i < (1 << N); i++) {
        for (int j = 0; j <= T; j++) {
            dp2[i][j] = max(dp2[i][j], dp2[i - 1][j]);
            if (j >= tim[i])
                dp2[i][j] = max(dp2[i][j], dp2[i][j - tim[i]] + ma[i]);
        }
    }
    int ans = 0;
    for (int i = 0; i <= T; i++)
        ans = max(ans, dp2[(1 << N) - 1][i]);
    cout << ans << endl;
}
accuracy: 0.08333333333333333
time_ms: 50
memory_kb: 10320
final_rank: 20

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int n;
int x[8],y[8];
int dp[1<<8][8];
int min_time[1<<7];

void TSP(){
    n++;
    for(int i=0;i<(1<<n);i++){
        for(int j=0;j<n;j++){
            dp[i][j] = (1<<30);
        }
    }
    n--;
    dp[0][n] = 0;
    n++;
    for(int i=0;i<(1<<n);i++){
        for(int j=0;j<n;j++){
            if(dp[i][j] == (1<<30)) continue;
            for(int k=0;k<n;k++){
                if((1<<k)&i)continue;
                int nx = (1<<k)|i;
                int cost = dp[i][j] + abs(x[j]-x[k]) + abs(y[j]-y[k]);
                dp[nx][k] = min(dp[nx][k], cost);
            }
        }
    } 
    n--;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int m,W,T; cin >> n >> m >> W >> T;
    map<string,int> to_id;
    vector<int> v(m),p(m);
    for(int i=0;i<m;i++){
        string s; cin >> s;
        to_id[s] = i;
        cin >> v[i] >> p[i];
    }
    vector<vector<pair<int,int>>> pro(n);
    for(int i=0;i<n;i++){
        int L; cin >> L;
        cin >> x[i] >> y[i];
        for(int j=0;j<L;j++){
            string r; cin >> r;
            int q; cin >> q;
            pro[i].push_back({to_id[r],q});
        }
    }
    x[n] = y[n] = 0;
    TSP();
    for(int i=1;i<(1<<n);i++){
        min_time[i] = dp[i+(1<<n)][n];
    }
    vector<int> opt(1<<n);
    for(int i=1;i<(1<<n);i++){
        if(min_time[i]>T)continue;
        int bit = 0;
        vector<int> cost(m,1<<30);
        for(int j=0;j<n;j++){
            if((1<<j)&i){
                for(auto &p:pro[j]){
                    bit |= (1<<p.first);
                    cost[p.first] = min(cost[p.first],p.second);
                }
            }
        }
        vector<ll> dp2(W+1);
        for(int j=1;j<=W;j++){
            dp2[j] = max(dp2[j], dp2[j-1]);
            for(int k=0;k<m;k++){
                if((1<<k)&bit){
                    if(j>=v[k]){
                        dp2[j] = max(dp2[j], dp2[j-v[k]]+p[k]-cost[k]);
                    }
                }
            }
        }
        opt[i] = dp2.back();
    }
    vector<ll> dp3(T+1);
    for(int i=1;i<=T;i++){
        dp3[i] = max(dp3[i],dp3[i-1]);
        for(int j=1;j<(1<<n);j++){
            if(i-min_time[j]>=0){
                dp3[i] = max(dp3[i], dp3[i-min_time[j]]+opt[j]);
            }
        }
    }
    cout << dp3.back() << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3536
final_rank: 2

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int n;
int x[8],y[8];
int dp[1<<8][8];
int min_time[1<<7];

void TSP(){
    n++;
    for(int i=0;i<(1<<n);i++){
        for(int j=0;j<n;j++){
            dp[i][j] = (1<<30);
        }
    }
    n--;
    dp[0][n] = 0;
    n++;
    for(int i=0;i<(1<<n);i++){
        for(int j=0;j<n;j++){
            if(dp[i][j] == (1<<30)) continue;
            for(int k=0;k<n;k++){
                if((1<<k)&i)continue;
                int nx = (1<<k)|i;
                int cost = dp[i][j] + abs(x[j]-x[k]) + abs(y[j]-y[k]);
                dp[nx][k] = min(dp[nx][k], cost);
            }
        }
    } 
    n--;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int m,W,T; cin >> n >> m >> W >> T;
    map<string,int> to_id;
    vector<int> v(m),p(m);
    for(int i=0;i<m;i++){
        string s; cin >> s;
        to_id[s] = i;
        cin >> v[i] >> p[i];
    }
    vector<vector<pair<int,int>>> pro(n);
    for(int i=0;i<n;i++){
        int L; cin >> L;
        cin >> x[i] >> y[i];
        for(int j=0;j<L;j++){
            string r; cin >> r;
            int q; cin >> q;
            pro[i].push_back({to_id[r],q});
        }
    }
    x[n] = y[n] = 0;
    TSP();
    for(int i=1;i<(1<<n);i++){
        min_time[i] = dp[i+(1<<n)][n];
    }
    vector<int> opt(1<<n);
    for(int i=1;i<(1<<n);i++){
        if(min_time[i]>T)continue;
        int bit = 0;
        vector<int> cost(m,1<<30);
        for(int j=0;j<n;j++){
            if((1<<j)&i){
                for(auto &p:pro[j]){
                    bit |= (1<<p.first);
                    cost[p.first] = min(cost[p.first],p.second);
                }
            }
        }
        vector<int> dp2(W+1);
        for(int j=1;j<=W;j++){
            dp2[j] = max(dp2[j], dp2[j-1]);
            for(int k=0;k<m;k++){
                if((1<<k)&bit){
                    if(j>=v[k]){
                        dp2[j] = max(dp2[j], dp2[j-v[k]]+p[k]-cost[k]);
                    }
                }
            }
        }
        opt[i] = dp2.back();
    }
    vector<int> dp3(T+1);
    for(int i=1;i<=T;i++){
        dp3[i] = max(dp3[i],dp3[i-1]);
        for(int j=1;j<(1<<n);j++){
            if(i-min_time[j]>=0){
                dp3[i] = max(dp3[i], dp3[i-min_time[j]]+opt[j]);
            }
        }
    }
    cout << dp3.back() << endl;
}
accuracy: 0.08333333333333333
time_ms: 10
memory_kb: 3444
final_rank: 14

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/all>
//using namespace atcoder;
//const int mod = 1e9 + 7;
const int mod = 998244353;

int main(){
    int n,m,w,t; cin >> n >> m >> w >> t;
    map<string,int> mp;
    vl v(m),p(m);
    rep(i,m){
        string s;
        cin >> s >> v[i] >> p[i];
        mp[s] = i;
    }
    vector<vpl> z(n);
    vl x(n),y(n);
    rep(i,n){
        int l; cin >> l;
        cin >> x[i] >> y[i];
        z[i].resize(l);
        rep(j,l){
            string r; cin >> r;
            int q; cin >> q;
            z[i][j] = {mp[r],q};
        }
    }
    vl dp(t+1,-linf);
    dp[0] = 0;
    REP(bit,1,1<<n){
        vl a;
        rep(i,n) if(bit>>i & 1) a.push_back(i);
        ll d = inf;
        int m = a.size();
        do{
            ll cd = abs(x[a[0]]) + abs(y[a[0]]);
            rep(i,m-1){
                cd += abs(x[a[i]]-x[a[i+1]]) + abs(y[a[i]]-y[a[i+1]]);
            }
            cd += abs(x[a[m-1]]) + abs(y[a[m-1]]);
            chmin(d,cd);
        }while(next_permutation(all(a)));
        vl ep(w+1,-linf);
        ep[0] = 0;
        rep(i,n){
            if(bit>>i & 1 ^ 1) continue;
            for(auto b : z[i]){
                rep(j,w){
                    if(j+v[b.first] > w) break;
                    chmax(ep[j+v[b.first]], ep[j] + p[b.first] - b.second);
                }
            }
        }
        ll mx = *max_element(all(ep));
        rep(i,t){
            if(i+d > t) break;
            chmax(dp[i+d], dp[i]+mx);
        }
    }
    cout << *max_element(all(dp)) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3596
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>

using namespace std;
using lint = long long;

constexpr int INF = 1 << 30;

void solve() {
    int n, m, wmax, t;
    cin >> n >> m >> wmax >> t;

    map<string, int> id;
    vector<int> ws(m);
    vector<lint> vs(m);
    for (int i = 0; i < m; ++i) {
        string s;
        cin >> s >> ws[i] >> vs[i];
        id[s] = i;
    }

    vector<pair<int, int>> pos(n);
    vector<vector<pair<int, lint>>> pss(n);  // weight, value
    for (int i = 0; i < n; ++i) {
        int k;
        {
            auto& [x, y] = pos[i];
            cin >> k >> x >> y;
        }

        while (k--) {
            string s;
            lint v;
            cin >> s >> v;
            int j = id[s];

            v = vs[j] - v;
            if (v > 0) pss[i].emplace_back(ws[j], v);
        }
    }

    vector<pair<int, lint>> ps;  // dist, value
    for (int b = 1; b < (1 << n); ++b) {
        vector<int> is;
        for (int i = 0; i < n; ++i) {
            if ((b >> i) & 1) is.push_back(i);
        }

        int dmin = INF;
        do {
            int px = 0, py = 0;

            int d = 0;
            for (auto i : is) {
                auto [x, y] = pos[i];
                d += abs(x - px) + abs(y - py);
                px = x, py = y;
            }

            d += abs(px) + abs(py);
            dmin = min(dmin, d);
        } while (next_permutation(is.begin(), is.end()));

        vector<lint> dp(wmax + 1, 0);
        for (auto i : is) {
            for (auto [w, v] : pss[i]) {
                for (int x = w; x <= wmax; ++x) {
                    dp[x] = max(dp[x], dp[x - w] + v);
                }
            }
        }

        ps.emplace_back(dmin, dp.back());
    }

    vector<lint> dp(t + 1, 0);
    for (auto [d, v] : ps) {
        for (int x = d; x <= t; ++x) {
            dp[x] = max(dp[x], dp[x - d] + v);
        }
    }
    cout << dp.back() << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3624
final_rank: 8

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

#define int long long
struct G{int w,v;};
template<typename T>
void chmax(T &a,T b){
  if(a<b)a=b;
}
template<typename T>
void chmin(T &a,T b){
  if(a>b)a=b;
}

signed main(){
  int n,m,W,T;cin>>n>>m>>W>>T;
  map<string,int> mp;
  vector<int> v(m),p(m);
  REP(i,m){
    string s;cin>>s>>v[i]>>p[i];
    mp[s]=i;
  }
  vector<int> x(n),y(n);
  vector<G> s[n];
  REP(i,n){
    int l;cin>>l>>x[i]>>y[i];
    REP(j,l){
      string tmp;int tmpv;
      cin>>tmp>>tmpv;
      if(p[mp[tmp]]>tmpv)
        s[i].push_back({v[mp[tmp]],p[mp[tmp]]-tmpv});
    }
  }
  vector<int> tme(1<<n,T+1),idx(n);
  tme[0]=0;
  REP(i,n)idx[i]=i;
  do{
    int visit=0,nowtime=0,nowy=0,nowx=0;
    REP(i,n){
      visit+=1<<idx[i];
      nowtime+=abs(nowy-y[idx[i]])+abs(nowx-x[idx[i]]);
      nowy=y[idx[i]],nowx=x[idx[i]];
      chmin(tme[visit],nowtime+abs(nowy)+abs(nowx));
    }
  }while(next_permutation(ALL(idx)));
  vector<int> va(T+1,0);
  REP(bit,1<<n){
    if(tme[bit]>T)continue;
    vector<int> a(W+1,0);
    REP(i,n)
      if((bit>>i)&1)
        for(auto g:s[i])
          REP(j,W-g.w+1)chmax(a[j+g.w],a[j]+g.v);
    chmax(va[tme[bit]],a[W]);
  }
  vector<int> ans(T+1,0);
  REP(i,T+1)
    REP(j,T+1-i)
      chmax(ans[i+j],ans[j]+va[i]);
  cout<<ans[T]<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3344
final_rank: 9

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

struct G{int w,v;};
template<typename T>
void chmax(T &a,T b){
  if(a<b)a=b;
}
template<typename T>
void chmin(T &a,T b){
  if(a>b)a=b;
}

signed main(){
  int n,m,W,T;cin>>n>>m>>W>>T;
  map<string,int> mp;
  vector<int> v(m),p(m);
  REP(i,m){
    string s;cin>>s>>v[i]>>p[i];
    mp[s]=i;
  }
  vector<int> x(n),y(n);
  vector<G> s[n];
  REP(i,n){
    int l;cin>>l>>x[i]>>y[i];
    REP(j,l){
      string tmp;int tmpv;
      cin>>tmp>>tmpv;
      if(p[mp[tmp]]>tmpv)
        s[i].push_back({v[mp[tmp]],p[mp[tmp]]-tmpv});
    }
  }
  vector<int> tme(1<<n,T+1),idx(n);
  tme[0]=0;
  REP(i,n)idx[i]=i;
  do{
    int visit=0,nowtime=0,nowy=0,nowx=0;
    REP(i,n){
      visit+=1<<idx[i];
      nowtime+=abs(nowy-y[idx[i]])+abs(nowx-x[idx[i]]);
      nowy=y[idx[i]],nowx=x[idx[i]];
      chmin(tme[visit],nowtime+abs(nowy)+abs(nowx));
    }
  }while(next_permutation(ALL(idx)));
  vector<int> va(T+1,0);
  REP(bit,1<<n){
    if(tme[bit]>T)continue;
    vector<int> a(W+1,0);
    REP(i,n)
      if((bit>>i)&1)
        for(auto g:s[i])
          REP(j,W-g.w+1)chmax(a[j+g.w],a[j]+g.v);
    chmax(va[tme[bit]],a[W]);
  }
  vector<int> ans(T+1,0);
  REP(i,T+1)
    REP(j,T+1-i)
      chmax(ans[i+j],ans[j]+va[i]);
  cout<<ans[T]<<endl;
}
accuracy: 0.08333333333333333
time_ms: 50
memory_kb: 3260
final_rank: 17

code_snippet: #include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
int N,M,W,T;
map<string,int>sell,we;
int X[7],Y[7];
vector<pair<string,int> >buy[7];
int dist[1<<7][7];
long ge[10101];
long w[1<<7][10101];
main()
{
	cin>>N>>M>>W>>T;
	for(int i=0;i<M;i++)
	{
		string s;cin>>s;
		cin>>we[s]>>sell[s];
	}
	for(int i=0;i<N;i++)
	{
		int L;cin>>L>>X[i]>>Y[i];
		for(int j=0;j<L;j++)
		{
			string t;int p;
			cin>>t>>p;
			buy[i].push_back(make_pair(t,p));
		}
	}
	for(int i=0;i<1<<N;i++)for(int j=0;j<N;j++)dist[i][j]=1e9;
	for(int i=0;i<N;i++)dist[1<<i][i]=abs(X[i])+abs(Y[i]);
	for(int i=1;i<1<<N;i++)for(int j=0;j<N;j++)if(i>>j&1)
	{
		for(int k=0;k<N;k++)
		{
			dist[i|1<<k][k]=min(dist[i|1<<k][k],dist[i][j]+abs(X[j]-X[k])+abs(Y[j]-Y[k]));
		}
	}
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<1<<i;j++)
		{
			int nj=j|1<<i;
			for(int k=0;k<=W;k++)w[nj][k]=w[j][k];
			for(pair<string,int>p:buy[i])
			{
				int wgh=we[p.first];
				int gt=sell[p.first]-p.second;
				for(int k=0;k<=W-wgh;k++)w[nj][k+wgh]=max(w[nj][k+wgh],w[nj][k]+gt);
			}
		}
	}
	for(int i=1;i<1<<N;i++)
	{
		int tm=1e9;
		for(int j=0;j<N;j++)tm=min(tm,dist[i][j]+abs(X[j])+abs(Y[j]));
		long B=w[i][W];
		for(int j=0;j<=T-tm;j++)ge[j+tm]=max(ge[j+tm],ge[j]+B);
	}
	cout<<ge[T]<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 13292
final_rank: 11