808D - Array Division
15 Jan 2021 — Tags: greedy,binary_search,implementation — URLTime complexity: $O(n \log{n})$
Memory complexity: $O(n)$
Click to show code.
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
bool solve(vi a)
{
ll target = accumulate(begin(a), end(a), 0LL);
if (target & 1)
return false;
target /= 2;
for (int i = 0; i < 2; ++i)
{
ll sum = 0;
set<ll> unique;
for (auto ai : a)
{
sum += ai;
unique.insert(ai);
if (unique.find(sum - target) != unique.end())
return true;
}
reverse(begin(a), end(a));
}
return false;
}
int main(void)
{
ios::sync_with_stdio(false), cin.tie(NULL);
int n;
cin >> n;
vi a(n);
for (auto &ai : a)
cin >> ai;
cout << (solve(a) ? "YES" : "NO") << endl;
return 0;
}