#include <iostream> #include <string> #include <cmath> #include <vector> #include <algorithm> using namespace std; int main() { int i, k, n; double s = 0, j; cin >> n; vector <int> v(n); for (i = 0; i < n; ++i) { cin >> v[i]; s += v[i]; } s /= 2; j = s; sort(v.begin(), v.end()); i = 0; while (s >= 0) { s -= v[i]; ++i; } k = i; int z = 0; i = n - 1; while (j >= 0) { s -= v[i]; ++z; --i; } if (n == 1) cout << 1; else { if (k < z) cout << (k + 1); else cout << (z + 1); } //system("pause"); return 0; } 

    1 answer 1

    Fun program you have ...
    Suppose, after entering and

     s /= 2; j = s; 

    we get j>0 . And how much after this this cycle will work:

     while (j >= 0) { s -= v[i]; ++z; --i; } 

    Exactly until i < 0 - i.e. go beyond the border of the array ... which you demonstrated in the figure.

    • Yes, thank you so much, I kind of rechecked everything, but apparently not to the end - Paul Shipilov