File size: 10,565 Bytes
14c9c2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <sstream>
#include <cstring>

using namespace std;

typedef long long ll;

const ll M = 20000000;   // mass limit in mg
const ll L = 25000000;   // volume limit in µl

struct Group {
    ll mass, vol, value;
    int type;
};

int main() {
    // Read entire input
    string s;
    char ch;
    while (cin.get(ch)) s += ch;

    // Parse JSON
    vector<string> names;
    vector<ll> q, v, m, l;
    int pos = 0;
    auto skip_ws = [&]() {
        while (pos < (int)s.size() && isspace(s[pos])) pos++;
    };
    skip_ws();
    if (s[pos] != '{') return 1;
    pos++;
    for (int i = 0; i < 12; ++i) {
        skip_ws();
        if (s[pos] != '"') return 1;
        pos++;
        string key;
        while (pos < (int)s.size() && s[pos] != '"') key += s[pos++];
        if (s[pos] != '"') return 1;
        pos++;
        skip_ws();
        if (s[pos] != ':') return 1;
        pos++;
        skip_ws();
        if (s[pos] != '[') return 1;
        pos++;
        vector<ll> nums;
        for (int j = 0; j < 4; ++j) {
            skip_ws();
            ll num = 0;
            bool neg = false;
            if (s[pos] == '-') { neg = true; pos++; }
            while (pos < (int)s.size() && isdigit(s[pos])) {
                num = num * 10 + (s[pos] - '0');
                pos++;
            }
            if (neg) num = -num;
            nums.push_back(num);
            skip_ws();
            if (j < 3) {
                if (s[pos] != ',') return 1;
                pos++;
            }
        }
        skip_ws();
        if (s[pos] != ']') return 1;
        pos++;
        skip_ws();
        if (i < 11) {
            if (s[pos] != ',') return 1;
            pos++;
        }
        names.push_back(key);
        q.push_back(nums[0]);
        v.push_back(nums[1]);
        m.push_back(nums[2]);
        l.push_back(nums[3]);
    }
    skip_ws();
    if (s[pos] != '}') return 1;

    int n = 12;

    // Precompute mask values
    int total_masks = 1 << n;
    vector<ll> mass_mask(total_masks, 0), vol_mask(total_masks, 0), value_mask(total_masks, 0);
    for (int mask = 0; mask < total_masks; ++mask) {
        ll mass = 0, vol = 0, val = 0;
        for (int i = 0; i < n; ++i) {
            if (mask & (1 << i)) {
                mass += q[i] * m[i];
                vol += q[i] * l[i];
                val += q[i] * v[i];
            }
        }
        mass_mask[mask] = mass;
        vol_mask[mask] = vol;
        value_mask[mask] = val;
    }

    // Best solution found
    ll best_value = -1;
    vector<ll> best_counts(n, 0);

    // 1. All variables at bounds (0 or q[i])
    for (int mask = 0; mask < total_masks; ++mask) {
        if (mass_mask[mask] <= M && vol_mask[mask] <= L) {
            if (value_mask[mask] > best_value) {
                best_value = value_mask[mask];
                for (int i = 0; i < n; ++i)
                    best_counts[i] = (mask & (1 << i)) ? q[i] : 0;
            }
        }
    }

    // 2. One free variable
    for (int mask = 0; mask < total_masks; ++mask) {
        for (int i = 0; i < n; ++i) {
            int fixed_mask = mask & ~(1 << i);
            ll mass_fixed = mass_mask[fixed_mask];
            ll vol_fixed = vol_mask[fixed_mask];
            ll value_fixed = value_mask[fixed_mask];
            ll rm = M - mass_fixed;
            ll rv = L - vol_fixed;
            if (rm < 0 || rv < 0) continue;
            ll max_xi = q[i];
            if (m[i] > 0) max_xi = min(max_xi, rm / m[i]);
            if (l[i] > 0) max_xi = min(max_xi, rv / l[i]);
            if (max_xi >= 0) {
                ll total_value = value_fixed + max_xi * v[i];
                if (total_value > best_value) {
                    best_value = total_value;
                    for (int k = 0; k < n; ++k)
                        best_counts[k] = (fixed_mask & (1 << k)) ? q[k] : 0;
                    best_counts[i] = max_xi;
                }
            }
        }
    }

    // 3. Two free variables
    for (int mask = 0; mask < total_masks; ++mask) {
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                int fixed_mask = mask & ~(1 << i) & ~(1 << j);
                ll mass_fixed = mass_mask[fixed_mask];
                ll vol_fixed = vol_mask[fixed_mask];
                ll value_fixed = value_mask[fixed_mask];
                ll M_res = M - mass_fixed;
                ll L_res = L - vol_fixed;
                if (M_res < 0 || L_res < 0) continue;
                double det = (double)m[i] * l[j] - (double)m[j] * l[i];
                if (fabs(det) < 1e-9) continue;
                double xi_cont = (M_res * l[j] - L_res * m[j]) / det;
                double xj_cont = (m[i] * L_res - l[i] * M_res) / det;
                for (int xi_round = 0; xi_round <= 1; ++xi_round) {
                    ll xi = (xi_round == 0) ? (ll)floor(xi_cont) : (ll)ceil(xi_cont);
                    if (xi < 0) xi = 0;
                    if (xi > q[i]) xi = q[i];
                    for (int xj_round = 0; xj_round <= 1; ++xj_round) {
                        ll xj = (xj_round == 0) ? (ll)floor(xj_cont) : (ll)ceil(xj_cont);
                        if (xj < 0) xj = 0;
                        if (xj > q[j]) xj = q[j];
                        ll mass = mass_fixed + xi * m[i] + xj * m[j];
                        ll volume = vol_fixed + xi * l[i] + xj * l[j];
                        if (mass <= M && volume <= L) {
                            ll total_value = value_fixed + xi * v[i] + xj * v[j];
                            if (total_value > best_value) {
                                best_value = total_value;
                                for (int k = 0; k < n; ++k)
                                    best_counts[k] = (fixed_mask & (1 << k)) ? q[k] : 0;
                                best_counts[i] = xi;
                                best_counts[j] = xj;
                            }
                        }
                    }
                }
            }
        }
    }

    // Try to improve the solution by filling remaining capacity
    ll total_mass = 0, total_vol = 0;
    for (int i = 0; i < n; ++i) {
        total_mass += best_counts[i] * m[i];
        total_vol  += best_counts[i] * l[i];
    }
    ll rm = M - total_mass;
    ll rv = L - total_vol;

    if (rm > 0 && rv > 0) {
        // If remaining capacities are small, use DP
        if (rm <= 2000 && rv <= 2500) {
            vector<Group> groups;
            for (int i = 0; i < n; ++i) {
                ll rq_i = q[i] - best_counts[i];
                if (rq_i <= 0) continue;
                ll k = rq_i;
                for (ll p = 1; p <= k; p <<= 1) {
                    ll take = min(p, k);
                    groups.push_back({ m[i] * take, l[i] * take, v[i] * take, i });
                    k -= take;
                }
            }
            int RM = (int)rm;
            int RV = (int)rv;
            vector<vector<ll>> dp(RM + 1, vector<ll>(RV + 1, -1));
            vector<vector<int>> from_group(RM + 1, vector<int>(RV + 1, -1));
            dp[0][0] = 0;
            for (int g = 0; g < (int)groups.size(); ++g) {
                Group &gr = groups[g];
                if (gr.mass > RM || gr.vol > RV) continue;
                for (int x = RM; x >= gr.mass; --x) {
                    for (int y = RV; y >= gr.vol; --y) {
                        if (dp[x - gr.mass][y - gr.vol] != -1) {
                            ll new_val = dp[x - gr.mass][y - gr.vol] + gr.value;
                            if (new_val > dp[x][y]) {
                                dp[x][y] = new_val;
                                from_group[x][y] = g;
                            }
                        }
                    }
                }
            }
            // Find best additional value
            ll best_add = 0;
            int best_x = 0, best_y = 0;
            for (int x = 0; x <= RM; ++x) {
                for (int y = 0; y <= RV; ++y) {
                    if (dp[x][y] > best_add) {
                        best_add = dp[x][y];
                        best_x = x;
                        best_y = y;
                    }
                }
            }
            if (best_add > 0) {
                vector<ll> add_counts(n, 0);
                int x = best_x, y = best_y;
                while (from_group[x][y] != -1) {
                    int g = from_group[x][y];
                    add_counts[groups[g].type] += groups[g].value / v[groups[g].type];
                    int nx = x - groups[g].mass;
                    int ny = y - groups[g].vol;
                    x = nx; y = ny;
                }
                for (int i = 0; i < n; ++i) {
                    best_counts[i] += add_counts[i];
                    best_value += add_counts[i] * v[i];
                }
            }
        } else {
            // Greedy fill based on value density
            vector<int> indices;
            for (int i = 0; i < n; ++i)
                if (best_counts[i] < q[i]) indices.push_back(i);
            sort(indices.begin(), indices.end(), [&](int a, int b) {
                double den_a = (double)v[a] / ( (double)m[a]/M + (double)l[a]/L );
                double den_b = (double)v[b] / ( (double)m[b]/M + (double)l[b]/L );
                return den_a > den_b;
            });
            vector<ll> temp_counts = best_counts;
            ll temp_mass = total_mass, temp_vol = total_vol;
            ll temp_rm = rm, temp_rv = rv;
            for (int i : indices) {
                ll can_add = min(q[i] - temp_counts[i],
                                 min(temp_rm / m[i], temp_rv / l[i]));
                if (can_add > 0) {
                    temp_counts[i] += can_add;
                    temp_mass += can_add * m[i];
                    temp_vol  += can_add * l[i];
                    temp_rm   -= can_add * m[i];
                    temp_rv   -= can_add * l[i];
                }
            }
            ll temp_value = 0;
            for (int i = 0; i < n; ++i) temp_value += temp_counts[i] * v[i];
            if (temp_value > best_value) {
                best_value = temp_value;
                best_counts = temp_counts;
            }
        }
    }

    // Output JSON
    cout << "{\n";
    for (int i = 0; i < n; ++i) {
        cout << " \"" << names[i] << "\": " << best_counts[i];
        if (i != n - 1) cout << ",";
        cout << "\n";
    }
    cout << "}\n";

    return 0;
}