Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ull unsigned ll
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define cin(v) for (auto&i:v) cin >> i;
- #define cout(v) for (auto&i:v) cout << i << " ";
- #define clr(memo, x) memset(memo, x, sizeof memo)
- #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- void Zainab(){
- // #ifndef ONLINE_JUDGE
- freopen("longpath.in", "r", stdin);
- freopen("longpath.out", "w", stdout);
- // #endif
- }
- void myCode( int test){
- int n ;
- cin >> n ;
- vector < ll > v(n) , suff(n+2,0);
- cin(v);
- sort(all(v));
- ll curr =0 , ans = -1e18;
- for(int i =0 ; i < n ; i++) {
- ans =max(ans , v[i]) ;
- curr += v[i] * (i+1);
- ans = max(ans , curr );
- }
- for(int i = n-1; i >=0 ; i--)
- suff[i] += suff[i+1] + v[i];
- for(int i =1 ; i < n ; i++){
- curr = curr - suff[i] - v[i-1];
- // if(curr <= ans) break;
- ans =max(ans , curr);
- }
- cout << ans;
- }
- int main(){
- FastCode ;
- // Zainab() ;
- int testCase=1;
- // cin >> testCase ;
- for(int i=1 ; i<= testCase ; i++)
- myCode(i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement