Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: C. Number of Ways
- // Contest: Codeforces - Codeforces Round 266 (Div. 2)
- // URL: https://codeforces.com/problemset/problem/466/C
- // Memory Limit: 256 MB
- // Time Limit: 2000 ms
- //
- // Powered by CP Editor (https://cpeditor.org)
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using a2l = array<ll, 2>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- void solve()
- {
- ll n;
- cin >> n;
- vl a(n);
- for (auto &x : a)
- cin >> x;
- ll tot = accumulate(a.begin(), a.end(), 0ll), ans = 0;
- if (tot % 3) {
- cout << 0 << '\n';
- return;
- }
- vl post1(n), post2(n);
- for (ll i = n - 1, sum = 0; i >= 0; --i) {
- sum += a[i];
- post1[i] = (i == n - 1 ? 0 : post1[i + 1]) + (sum == tot / 3);
- }
- for (ll i = n - 1, sum = 0; i >= 0; --i) {
- sum += a[i];
- post2[i] = (sum == tot / 3 * 2 ? (i == n - 1 ? 0 : post1[i + 1]) : 0);
- }
- for (ll i = 0, sum = 0; i < n; ++i) {
- sum += a[i];
- if (sum == tot / 3)
- ans += i + 1 < n ? post2[i + 1] : 0;
- }
- dbg(post1, post2);
- cout << ans << '\n';
- }
- int main(int argc, char **argv)
- {
- solve();
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement