Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- /*
- int solution(vector<int> &A) {
- int sum = 0, ans = -1;
- const int n = A.size();
- bool isNoNeg = false;
- for (int i = 0; i < n; i++) {
- if (A[i] < 0) {
- ans = max(ans, sum);
- sum = 0;
- } else {
- sum += A[i];
- isNoNeg = true;
- }
- }
- if (!isNoNeg) {
- return -1;
- }
- ans = max(ans, sum);
- return ans;
- }//*/
- int getPS(const vector<int> &ps, int i, int j) {
- int r = ps[j];
- if (i - 1 >= 0) {
- r -= ps[i - 1];
- }
- return r;
- }
- int solution(vector<int> &A) {
- const int n = A.size();
- const int MAXI = 1e4 + 3;
- vector<int> ps(n);
- vector<pair<int, int>> poss(MAXI, make_pair(-1, -1));
- ps[0] = A[0];
- poss[A[0]].first = 0;
- for (int i = 1; i < n; i++) {
- ps[i] = ps[i - 1] + A[i];
- if (poss[A[i]].first == -1) {
- poss[A[i]].first = i;
- } else {
- poss[A[i]].second = i;
- }
- }
- int ans = -1;
- for (int x = 1; x < MAXI; x++) {
- if (poss[x].second != -1) {
- ans = max(ans, getPS(ps, poss[x].first, poss[x].second));
- }
- }
- //cerr << ps[2] << endl;
- return ans;
- }//*/
- int main() {
- vector<int> v(2, 1);v[1]=2;
- cerr << solution(v) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement