Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<bits/stdc++.h>
- #include<iostream>
- #include<fstream>
- #include<vector>
- #include<unordered_map>
- #include<algorithm>
- #define long int
- #define nln '\n'
- const long N = 1e5;
- using namespace std;
- // Global variables: f1, f2, n, a
- fstream f1, f2;
- inline void openf()
- {
- f1.open("twosum.inp", ios:: in);
- f2.open("twosum.out", ios:: out);
- }
- inline void closef()
- {
- f1.close();
- f2.close();
- }
- long n;
- vector<long> a, sum;
- void data()
- {
- f1.tie(0)->sync_with_stdio(0);
- f2.tie(0)->sync_with_stdio(0);
- //cin.tie(0)->sync_with_stdio(0);
- cin >> n;
- a.resize(n+2, 0);
- sum.resize(n+2, 0);
- for (long i = 1; i-1 != n; ++i)
- {
- cin >> a[i];
- sum[i] = a[i] + sum[i-1];
- }
- }
- long total(long i, long j)
- {
- return sum[j] - sum[i-1];
- }
- template<typename T>
- void chmax(T &a, T b) {
- if (a < b) a = b;
- }
- long ans = 0;
- void process()
- {
- unordered_map<long, long> tic;
- tic[0] = 0;
- for (long i = 1; i <= n; i++)
- {
- for (long j = i+1; j <= n; j++)
- {
- long tol = sum[j]-sum[i];
- if (tic.count(sum[i]-tol))
- ans = max(ans, j-tic[sum[i]-tol]);
- }
- tic[sum[i]] = i;
- }
- }
- void view()
- {
- cout << ans << nln;
- }
- int main()
- {
- openf();
- data();
- process();
- view();
- closef();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment