B-显生之宙_牛客小白月赛89 (nowcoder.com)
题解:思路很简单,但是当时晕晕的,写拉了
题目要求最大,那么负数要让每一个数都加,正数只能加一个
我们正数加到最后一个数即可,负数累加
#include <bits/stdc++.h> //#pragma GCC optimize("Ofast") #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> //#define double long double #define int long long #define endl '\n' using namespace std; const int N=6e6+10,M=1e1; const int INF = 0x3f3f3f3f; const int mod=1e9+7; typedef pair<int,int> PII; int kmp(int a,int k,int p) { int ans=1; while (k) { if(k&1) ans=ans*a%p; k>>=1; a=a*a; } return ans; } int a[N]; void solve() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } sort(a+1,a+1+n); int sum=0; for(int i=1;i<=n;i++) { a[i]+=sum; if(a[i]<0) { sum+=a[i]; } else if(i!=n) { a[n]+=a[i]; } } cout<<a[n]<<endl; } signed main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int T=1; cin>>T; while(T--){ solve(); } return 0; }
标签:const,int,小白月赛,long,牛客,ans,include,define From: https://www.cnblogs.com/whatdo/p/18092533