Advertisement
Zeinab_Hamdy

Untitled

Mar 19th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV return void
  10. #define sz(x) int(x.size())
  11. #define all(v) v.begin(), v.end()
  12. #define rall(v) v.rbegin(), v.rend()
  13. #define fixed(n) fixed << setprecision(n)
  14. #define cin(v) for (auto&i:v) cin >> i;
  15. #define cout(v) for (auto&i:v) cout << i << " ";
  16. #define clr(memo, x) memset(memo, x, sizeof memo)
  17. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  18.  
  19. void Zainab(){
  20. // #ifndef ONLINE_JUDGE
  21. freopen("longpath.in", "r", stdin);
  22. freopen("longpath.out", "w", stdout);
  23. // #endif
  24. }
  25.  
  26.  
  27.  
  28. void myCode( int test){
  29.  
  30. int n ;
  31. cin >> n ;
  32. vector < ll > v(n) , suff(n+2,0);
  33. cin(v);
  34.  
  35. sort(all(v));
  36.  
  37. ll curr =0 , ans = -1e18;
  38.  
  39. for(int i =0 ; i < n ; i++) {
  40. ans =max(ans , v[i]) ;
  41. curr += v[i] * (i+1);
  42. ans = max(ans , curr );
  43. }
  44.  
  45.  
  46.  
  47. for(int i = n-1; i >=0 ; i--)
  48. suff[i] += suff[i+1] + v[i];
  49.  
  50.  
  51. for(int i =1 ; i < n ; i++){
  52. curr = curr - suff[i] - v[i-1];
  53.  
  54. // if(curr <= ans) break;
  55. ans =max(ans , curr);
  56. }
  57.  
  58. cout << ans;
  59.  
  60.  
  61. }
  62.  
  63.  
  64. int main(){
  65. FastCode ;
  66. // Zainab() ;
  67.  
  68. int testCase=1;
  69. // cin >> testCase ;
  70.  
  71.  
  72.  
  73. for(int i=1 ; i<= testCase ; i++)
  74. myCode(i);
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement