1 solutions

  • 0
    @ 2025-10-4 8:24:58

    显然,在完成哪个心愿结果相同的条件下,尽可能完成 wiw_i 更小的就可能完成更多心愿。

    因此,我们按照 wiw_i 从小到大排序,依次完成这些愿望,看最多能完成多少个即可。

    标程

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e5 + 5;
    
    int n, W, A[N];
    
    int main(){
        scanf("%d%d", &n, &W);
        for (int i = 1; i <= n; i++)
            scanf("%d", &A[i]);
        sort(A + 1, A + 1 + n);
        int ans = 0;
        for (int i = 1; i <= n && A[i] <= W; i++){
            W -= A[i];
            ans = i;
        }
        printf("%d\n", ans);
        return 0;
    }
    
    • 1

    Information

    ID
    46
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    (None)
    # Submissions
    14
    Accepted
    5
    Uploaded By