File size: 10,075 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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cctype>
#include <cmath>

using namespace std;

struct Item {
    string name;
    int q, v, m, l;
};

const long long Mmax = 20000000;
const long long Vmax = 25000000;

vector<Item> items;
vector<int> best_counts(12, 0);
long long best_value = 0;

void update_best(const vector<int>& counts) {
    long long value = 0;
    for (int i = 0; i < 12; ++i) {
        value += counts[i] * (long long)items[i].v;
    }
    if (value > best_value) {
        best_value = value;
        best_counts = counts;
    }
}

vector<int> greedy_fill(const vector<int>& order) {
    vector<int> counts(12, 0);
    long long cur_mass = 0, cur_vol = 0;
    for (int idx : order) {
        const Item& it = items[idx];
        int maxTake = min(it.q, (int)((Mmax - cur_mass) / it.m), (int)((Vmax - cur_vol) / it.l));
        if (maxTake > 0) {
            counts[idx] = maxTake;
            cur_mass += maxTake * (long long)it.m;
            cur_vol += maxTake * (long long)it.l;
        }
    }
    return counts;
}

void swap_local_search(vector<int>& counts, long long& cur_mass, long long& cur_vol) {
    bool improved = true;
    while (improved) {
        improved = false;
        for (int i = 0; i < 12 && !improved; ++i) {
            if (counts[i] >= items[i].q) continue;
            const Item& it_i = items[i];
            // 1-for-1 swap
            for (int j = 0; j < 12 && !improved; ++j) {
                if (counts[j] == 0) continue;
                const Item& it_j = items[j];
                if (cur_mass - it_j.m + it_i.m <= Mmax && cur_vol - it_j.l + it_i.l <= Vmax) {
                    if (it_i.v > it_j.v) {
                        counts[i]++; counts[j]--;
                        cur_mass += it_i.m - it_j.m;
                        cur_vol += it_i.l - it_j.l;
                        improved = true;
                    }
                }
            }
            if (improved) break;
            // 1-for-2 swap
            for (int j = 0; j < 12 && !improved; ++j) {
                if (counts[j] == 0) continue;
                const Item& it_j = items[j];
                for (int k = 0; k < 12 && !improved; ++k) {
                    if (counts[k] == 0) continue;
                    if (k == j && counts[j] < 2) continue;
                    const Item& it_k = items[k];
                    if (cur_mass - it_j.m - it_k.m + it_i.m <= Mmax &&
                        cur_vol - it_j.l - it_k.l + it_i.l <= Vmax) {
                        long long removed = it_j.v + (k == j ? it_j.v : it_k.v);
                        if (it_i.v > removed) {
                            counts[i]++; counts[j]--;
                            if (k == j) counts[j]--;
                            else counts[k]--;
                            cur_mass += it_i.m - it_j.m - it_k.m;
                            cur_vol += it_i.l - it_j.l - it_k.l;
                            improved = true;
                        }
                    }
                }
            }
        }
    }
}

void removal_reopt(const vector<int>& order, const vector<int>& base_counts,
                   long long base_mass, long long base_vol) {
    // subsets of size 1,2,3
    for (int sz = 1; sz <= 3; ++sz) {
        if (sz == 1) {
            for (int i = 0; i < 12; ++i) {
                vector<int> counts = base_counts;
                long long cur_mass = base_mass;
                long long cur_vol = base_vol;
                if (counts[i] > 0) {
                    cur_mass -= counts[i] * (long long)items[i].m;
                    cur_vol -= counts[i] * (long long)items[i].l;
                    counts[i] = 0;
                }
                for (int idx : order) {
                    if (idx == i) continue;
                    const Item& it = items[idx];
                    int maxTake = min(it.q - counts[idx],
                                      (int)((Mmax - cur_mass) / it.m),
                                      (int)((Vmax - cur_vol) / it.l));
                    if (maxTake > 0) {
                        counts[idx] += maxTake;
                        cur_mass += maxTake * (long long)it.m;
                        cur_vol += maxTake * (long long)it.l;
                    }
                }
                update_best(counts);
            }
        } else if (sz == 2) {
            for (int i = 0; i < 12; ++i) {
                for (int j = i+1; j < 12; ++j) {
                    vector<int> counts = base_counts;
                    long long cur_mass = base_mass;
                    long long cur_vol = base_vol;
                    for (int t : {i, j}) {
                        if (counts[t] > 0) {
                            cur_mass -= counts[t] * (long long)items[t].m;
                            cur_vol -= counts[t] * (long long)items[t].l;
                            counts[t] = 0;
                        }
                    }
                    for (int idx : order) {
                        if (idx == i || idx == j) continue;
                        const Item& it = items[idx];
                        int maxTake = min(it.q - counts[idx],
                                          (int)((Mmax - cur_mass) / it.m),
                                          (int)((Vmax - cur_vol) / it.l));
                        if (maxTake > 0) {
                            counts[idx] += maxTake;
                            cur_mass += maxTake * (long long)it.m;
                            cur_vol += maxTake * (long long)it.l;
                        }
                    }
                    update_best(counts);
                }
            }
        } else { // sz == 3
            for (int i = 0; i < 12; ++i) {
                for (int j = i+1; j < 12; ++j) {
                    for (int k = j+1; k < 12; ++k) {
                        vector<int> counts = base_counts;
                        long long cur_mass = base_mass;
                        long long cur_vol = base_vol;
                        for (int t : {i, j, k}) {
                            if (counts[t] > 0) {
                                cur_mass -= counts[t] * (long long)items[t].m;
                                cur_vol -= counts[t] * (long long)items[t].l;
                                counts[t] = 0;
                            }
                        }
                        for (int idx : order) {
                            if (idx == i || idx == j || idx == k) continue;
                            const Item& it = items[idx];
                            int maxTake = min(it.q - counts[idx],
                                              (int)((Mmax - cur_mass) / it.m),
                                              (int)((Vmax - cur_vol) / it.l));
                            if (maxTake > 0) {
                                counts[idx] += maxTake;
                                cur_mass += maxTake * (long long)it.m;
                                cur_vol += maxTake * (long long)it.l;
                            }
                        }
                        update_best(counts);
                    }
                }
            }
        }
    }
}

int main() {
    // Read entire input
    string s, line;
    while (getline(cin, line)) s += line;
    // Remove whitespace
    s.erase(remove_if(s.begin(), s.end(), ::isspace), s.end());
    // Parse JSON
    // Remove outer braces
    if (s.front() == '{') s = s.substr(1, s.size()-2);
    size_t pos = 0;
    while (pos < s.size()) {
        if (s[pos] != '"') break;
        size_t key_start = pos+1;
        size_t key_end = s.find('"', key_start);
        string key = s.substr(key_start, key_end - key_start);
        pos = key_end + 1;
        if (s[pos] != ':') break;
        pos++;
        if (s[pos] != '[') break;
        pos++;
        size_t array_end = s.find(']', pos);
        string array_str = s.substr(pos, array_end - pos);
        vector<int> nums;
        size_t comma = 0;
        while (true) {
            size_t next_comma = array_str.find(',', comma);
            if (next_comma == string::npos) {
                nums.push_back(stoi(array_str.substr(comma)));
                break;
            }
            nums.push_back(stoi(array_str.substr(comma, next_comma - comma)));
            comma = next_comma + 1;
        }
        items.push_back({key, nums[0], nums[1], nums[2], nums[3]});
        pos = array_end + 1;
        if (pos < s.size() && s[pos] == ',') pos++;
    }

    // Three metrics
    for (int metric = 1; metric <= 3; ++metric) {
        vector<int> order(12);
        iota(order.begin(), order.end(), 0);
        if (metric == 1) {
            sort(order.begin(), order.end(), [](int a, int b) {
                double da = items[a].v / (items[a].m / (double)Mmax + items[a].l / (double)Vmax);
                double db = items[b].v / (items[b].m / (double)Mmax + items[b].l / (double)Vmax);
                return da > db;
            });
        } else if (metric == 2) {
            sort(order.begin(), order.end(), [](int a, int b) {
                return (long long)items[a].v * items[b].m > (long long)items[b].v * items[a].m;
            });
        } else {
            sort(order.begin(), order.end(), [](int a, int b) {
                return (long long)items[a].v * items[b].l > (long long)items[b].v * items[a].l;
            });
        }

        vector<int> counts = greedy_fill(order);
        long long cur_mass = 0, cur_vol = 0;
        for (int i = 0; i < 12; ++i) {
            cur_mass += counts[i] * (long long)items[i].m;
            cur_vol += counts[i] * (long long)items[i].l;
        }
        swap_local_search(counts, cur_mass, cur_vol);
        update_best(counts);
        removal_reopt(order, counts, cur_mass, cur_vol);
    }

    // Output JSON
    cout << "{\n";
    for (int i = 0; i < 12; ++i) {
        cout << " \"" << items[i].name << "\": " << best_counts[i];
        if (i != 11) cout << ",";
        cout << "\n";
    }
    cout << "}" << endl;

    return 0;
}