fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5.  
  6.  
  7. const int M = 1000000007;
  8. const int N = 3e5+9;
  9. const int INF = 2e9+1;
  10. const int MAXN = 100000;
  11. const int LINF = 2000000000000000001;
  12.  
  13. //_ ***************************** START Below *******************************
  14.  
  15.  
  16.  
  17. vector<int> a;
  18.  
  19. //* Template 1
  20.  
  21. int consistency1(int n ){
  22.  
  23. int sum = 0, ans = 0;
  24. unordered_map<int,int> mp;
  25.  
  26. int s = 0, e = 0;
  27. while(e<n){
  28. mp[a[e]]++;
  29. sum += a[e];
  30.  
  31. if(mp.size() == e-s+1){
  32. ans = max(ans, sum);
  33. e++;
  34. }
  35. else{
  36. while(s<=e && mp.size() < e-s+1){
  37. mp[a[s]]--;
  38. if(mp[a[s]] == 0) mp.erase(a[s]);
  39. sum -= a[s];
  40. s++;
  41. }
  42. ans = max(ans, sum);
  43. e++;
  44. }
  45. }
  46.  
  47. return ans;
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. //* Template 2
  56.  
  57. int consistency2(int n ){
  58.  
  59. int sum = 0, ans = 0;
  60. unordered_map<int,int> mp;
  61.  
  62. for(int j=0, i=0; j<n; j++){
  63. mp[a[j]]++;
  64. sum += a[j];
  65.  
  66. while(i<=j && mp.size() < j-i+1){
  67. mp[a[i]]--;
  68. if(mp[a[i]] == 0) mp.erase(a[i]);
  69. sum -= a[i];
  70. i++;
  71. }
  72. ans = max(ans, sum);
  73. }
  74.  
  75. return ans;
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. int practice(int n){
  92.  
  93. return 0;
  94. }
  95.  
  96.  
  97.  
  98.  
  99. void solve() {
  100.  
  101. int n;
  102. cin >> n;
  103. a.resize(n);
  104. for(int i=0; i<n; i++) cin >> a[i];
  105.  
  106. cout << consistency1(n) << " " << consistency2(n) << endl;
  107. // cout << consistency1(n) << " -> " << practice(n) << endl;
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. int32_t main() {
  116. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  117.  
  118. int t = 1;
  119. while (t--) {
  120. solve();
  121. }
  122.  
  123. return 0;
  124. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
4 2 4 5 6
stdout
17 17