Submission #72749115


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
pair<ll, vector<pair<ll, ll>>> mergeItems(vector<pair<ll, ll>> items, int removeCount, int groupSize) {
    ll removedValueSum = 0;
    while (removeCount > 0 && !items.empty()) {
        auto [value, count] = items.back();
        if (count > removeCount) {
            removedValueSum += value * removeCount;
            items.back().second -= removeCount;
            break;
        }
        removedValueSum += value * count;
        removeCount -= count;
        items.pop_back();
    }
    
    vector<pair<ll, ll>> mergedItems;
    ll currentCount = 0, currentSum = 0;
    while (!items.empty()) {
        auto [value, count] = items.back();
        items.pop_back();
        
        if (currentCount > 0) {
            if (currentCount + count < groupSize) {
                currentCount += count;
                currentSum += value * count;
                continue;
            }
            count -= groupSize - currentCount;
            currentSum += value * (groupSize - currentCount);
            mergedItems.emplace_back(currentSum, 1);
            currentCount = currentSum = 0;
        }
        

        if (count >= groupSize) {
            mergedItems.emplace_back(value * groupSize, count / groupSize);
        }
        
        currentCount = count % groupSize;
        currentSum = value * currentCount;
    }
    
    return {removedValueSum, mergedItems};
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int itemTypes;
    ll capacity;
    cin >> itemTypes >> capacity;
    
  
    vector<vector<pair<ll, ll>>> weightClasses(4);
    
    for (int i = 0; i < itemTypes; i++) {
        ll weight, value, count;
        cin >> weight >> value >> count;
        weightClasses[weight].emplace_back(value, count);
    }
    

    for (int weight = 1; weight <= 3; weight++) {
        sort(weightClasses[weight].begin(), weightClasses[weight].end());
    }
    
    ll maxTotalValue = 0;

    for (int weight1Remove = 0; weight1Remove < 6; weight1Remove++) {
        auto [removedValue1, mergedItems1] = mergeItems(weightClasses[1], weight1Remove, 6);
        
        for (int weight2Remove = 0; weight2Remove < 3; weight2Remove++) {
            auto [removedValue2, mergedItems2] = mergeItems(weightClasses[2], weight2Remove, 3);
            
            for (int weight3Remove = 0; weight3Remove < 2; weight3Remove++) {
                auto [removedValue3, mergedItems3] = mergeItems(weightClasses[3], weight3Remove, 2);
                
            
                ll remainingCapacity = capacity - weight1Remove - weight2Remove * 2 - weight3Remove * 3;
                if (remainingCapacity < 0) continue;
                
                remainingCapacity /= 6; 
                ll currentValue = removedValue1 + removedValue2 + removedValue3;
                
    
                vector<pair<ll, ll>> allItems = mergedItems1;
                allItems.insert(allItems.end(), mergedItems2.begin(), mergedItems2.end());
                allItems.insert(allItems.end(), mergedItems3.begin(), mergedItems3.end());
                
                sort(allItems.begin(), allItems.end());
                
  
                while (!allItems.empty() && remainingCapacity > 0) {
                    auto [value, count] = allItems.back();
                    allItems.pop_back();
                    
                    if (count >= remainingCapacity) {
                        currentValue += value * remainingCapacity;
                        break;
                    }
                    remainingCapacity -= count;
                    currentValue += value * count;
                }
                
                maxTotalValue = max(maxTotalValue, currentValue);
            }
        }
    }
    
    cout << maxTotalValue << endl;
    return 0;
}

Submission Info

Submission Time
Task G - Lightweight Knapsack
User W105202wwc
Language C++23 (GCC 15.2.0)
Score 600
Code Size 3995 Byte
Status AC
Exec Time 658 ms
Memory 25776 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 58
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 01_small_08.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 03_special_00.txt, 03_special_01.txt, 03_special_02.txt, 03_special_03.txt, 03_special_04.txt, 03_special_05.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 05_killer_00.txt, 05_killer_01.txt, 05_killer_02.txt, 05_killer_03.txt, 05_killer_04.txt, 06_killer2_00.txt, 06_killer2_01.txt, 07_killer3_00.txt, 07_killer3_01.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3732 KiB
00_sample_01.txt AC 1 ms 3668 KiB
00_sample_02.txt AC 1 ms 3668 KiB
01_small_00.txt AC 1 ms 3796 KiB
01_small_01.txt AC 1 ms 3712 KiB
01_small_02.txt AC 1 ms 3760 KiB
01_small_03.txt AC 1 ms 3780 KiB
01_small_04.txt AC 1 ms 3812 KiB
01_small_05.txt AC 1 ms 3752 KiB
01_small_06.txt AC 1 ms 3688 KiB
01_small_07.txt AC 1 ms 3712 KiB
01_small_08.txt AC 2 ms 3752 KiB
02_random_00.txt AC 534 ms 25188 KiB
02_random_01.txt AC 516 ms 22908 KiB
02_random_02.txt AC 533 ms 23632 KiB
02_random_03.txt AC 620 ms 25200 KiB
02_random_04.txt AC 583 ms 22944 KiB
02_random_05.txt AC 592 ms 25408 KiB
02_random_06.txt AC 643 ms 25200 KiB
02_random_07.txt AC 605 ms 24460 KiB
02_random_08.txt AC 658 ms 25140 KiB
02_random_09.txt AC 534 ms 25360 KiB
02_random_10.txt AC 487 ms 23108 KiB
02_random_11.txt AC 514 ms 25392 KiB
02_random_12.txt AC 595 ms 25764 KiB
02_random_13.txt AC 588 ms 22900 KiB
02_random_14.txt AC 624 ms 23744 KiB
02_random_15.txt AC 651 ms 25324 KiB
02_random_16.txt AC 578 ms 24776 KiB
02_random_17.txt AC 614 ms 23884 KiB
02_random_18.txt AC 533 ms 25320 KiB
02_random_19.txt AC 485 ms 23124 KiB
02_random_20.txt AC 503 ms 25468 KiB
02_random_21.txt AC 588 ms 23928 KiB
02_random_22.txt AC 565 ms 24868 KiB
02_random_23.txt AC 634 ms 25216 KiB
02_random_24.txt AC 615 ms 25676 KiB
02_random_25.txt AC 582 ms 24616 KiB
02_random_26.txt AC 618 ms 25512 KiB
03_special_00.txt AC 1 ms 3760 KiB
03_special_01.txt AC 1 ms 3744 KiB
03_special_02.txt AC 1 ms 3584 KiB
03_special_03.txt AC 1 ms 3712 KiB
03_special_04.txt AC 1 ms 3760 KiB
03_special_05.txt AC 1 ms 3756 KiB
04_handmade_00.txt AC 87 ms 23236 KiB
04_handmade_01.txt AC 76 ms 9568 KiB
04_handmade_02.txt AC 1 ms 3608 KiB
04_handmade_03.txt AC 299 ms 23116 KiB
05_killer_00.txt AC 512 ms 25764 KiB
05_killer_01.txt AC 531 ms 25252 KiB
05_killer_02.txt AC 533 ms 25248 KiB
05_killer_03.txt AC 559 ms 25216 KiB
05_killer_04.txt AC 506 ms 25776 KiB
06_killer2_00.txt AC 297 ms 25400 KiB
06_killer2_01.txt AC 454 ms 25256 KiB
07_killer3_00.txt AC 386 ms 22424 KiB
07_killer3_01.txt AC 350 ms 22116 KiB