Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6.       int n, maxim = 0, cumm = 0;
  7.       vector < int >v;
  8.       cin >> n;
  9.       v.resize (n);
  10.       for (int i = 0; i < n; i++)
  11.         cin >> v[i];
  12.       sort (v.begin (), v.end ());
  13.       //if (n>1)
  14.       int left = 0;
  15.       int right = 1;
  16.       cumm = v[0] + v[1];
  17.       while (right <= n-1)
  18.         {
  19.               if (right < n - 1 && v[left] + v[left + 1] >= v[right + 1])
  20.               {
  21.  
  22.                   right += 1;
  23.                   cumm += v[right];
  24.                   if (cumm > maxim)
  25.                   {
  26.                     maxim = cumm;
  27.                   }
  28.                   else
  29.                   {
  30.                     cumm -= v[left];
  31.                     left += 1;
  32.                   }
  33.               }
  34.               cout << left << ' ' << right << endl;
  35.  
  36. }
  37. cout << maxim;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement