Advertisement
Josif_tepe

Untitled

Apr 22nd, 2023
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <map>
  7. using namespace std;
  8. const int maxn = 1005;
  9.  
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     int n;
  13.     cin >> n;
  14.    
  15.     vector<int> v(n);
  16.     for(int i = 0; i < n; i++) {
  17.         cin >> v[i];
  18.     }
  19.  
  20.     map<int, int> m;
  21.     int i = 0,j = 0;
  22.     int sum= 0, result = 0;
  23.     while(j < n and i < n) {
  24.         if(m[v[j]] < 1) {
  25.             sum += v[j];
  26.             m[v[j]]++;
  27.             j++;
  28.         }
  29.         else {
  30.             m[v[i]]--;
  31.             sum -= v[i];
  32.             i++;
  33.         }
  34.         result =max(result, sum);
  35.     }
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement