Advertisement
OIQ

IT

OIQ
Oct 21st, 2019
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.     int n;
  10.     cin >> n;
  11.  
  12.     vector <int> a(n);
  13.     for (int i = 0; i < n; i++)
  14.         cin >> a[i];
  15.  
  16.     int l = 0, r = a.size() - 1;
  17.     long sum1 = a[0], sum2 = a[a.size() - 1];
  18.     int min = abs(sum1 - sum2);
  19.     int minl = l, minr = r;
  20.  
  21.     while (l != r) {
  22.         if (sum1 > sum2) {
  23.             r--;
  24.             sum2 += a[r];
  25.         }
  26.         else {
  27.             l++;
  28.             sum1 += a[l];
  29.         }
  30.  
  31.         if (abs(sum1 - sum2) < min) {
  32.             min = abs(sum1 - sum2);
  33.             minl = l;
  34.             minr = r;
  35.         }
  36.     }
  37.  
  38.     cout << min << " " <<minl+1 << " "<< minr+1;
  39.     return 0;
  40. }
Advertisement
RAW Paste Data Copied
Advertisement