请问大佬们,B题这种写法过80%样例,是什么原因

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
using ll=long long;
using ff=double;
 
int main(void){
    IOS;
    ll T;
      cin>>T;
     
    while(T--){
        ll n;
          cin>>n;
        ll a[n+10];
         
        for(ll i=0;i<n;i++){
              cin>>a[i];
        }
        sort(a, a+n);
         
        ll cost=0, now=0, last=0, ans=0;
        ll i=0;
        while(a[i]<=abs(cost)){
            now=a[i]+last;
//               cout<<a[i]<<' '<<last<<' '<<now<<'\n';
            ans=now;
            last+=now;
             
            cost+=now;
            i++;
        }


        if(i!=n) ans=0;
        for(;i<n;i++){
            ans+=a[i]+cost;
        }
          cout<<ans<<'\n';
    }
    return 0;
}

全部评论
循环还要判断i < n
点赞 回复
分享
发布于 03-23 18:40 湖北

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务