1 条题解

  • 0
    @ 2026-9-30 12:59:15

    C++ :

    
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    /* 风险值 = 头上所有牛的重量和 - 自己的强壮度。
       结论:按 (w + s) 升序排列最优,答案 = max(前缀重量 - s_i)。 */
    struct Cow { long long w, s, k; };
    Cow c[50005];
    int main(){
        int n;
        if(scanf("%d",&n)!=1) return 0;
        for(int i=0;i<n;i++){
            scanf("%lld %lld",&c[i].w,&c[i].s);
            c[i].k = c[i].w + c[i].s;
        }
        sort(c, c+n, [](const Cow&a, const Cow&b){ return a.k < b.k; });
        long long sum=0, ans=-(1LL<<62);
        for(int i=0;i<n;i++){
            long long risk = sum - c[i].s;
            if(risk>ans) ans=risk;
            sum += c[i].w;
        }
        printf("%lld\n", ans);
        return 0;
    }
    
    
    • 1

    信息

    ID
    2852
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者