/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Wrong Answer 2ms 364.0 KiB
#3 Wrong Answer 369ms 824.0 KiB

Code

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
using namespace std;

void solve(int cs) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  unordered_map<int, vector<pair<int, int>>> who;
  for (int i = 0; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (a[j] > a[i]) {
        if (__gcd(a[i], a[j]) > 1) {
          who[a[i]].push_back({a[j], j});
        }
      }
    }
    for (int j = i + 1; j < n; j++) {
      if (a[j] > a[i]) {
        if (__gcd(a[i], a[j]) > 1) {
          who[a[i]].push_back({a[j], j});
        }
      }
    }
  }

  for (auto &[_, v] : who) {
    sort(v.rbegin(), v.rend());
  }
  int64_t res = 0;
  multiset<int> S;
  for (int i = 0; i < k; i++) {
    S.insert(a[i]);
  }
  for (int i = k - 1; i < n; i++) {
    if (i >= k) {
      S.insert(a[i]);
      S.erase(S.find(a[i - k]));
    }
    int64_t sum = 0;
    pair<int, int> range = {i - k, i};
    unordered_map<int, bool> taken;
    for (auto &it : S) {
      int got = it;
      for (auto &[val, id] : who[it]) {
        if (id > range.second || id < range.first) {
          if (!taken[id]) {
            got = val;
            taken[id] = true;
            break;
          }
        }
      }
      sum += got;
    }
    if (sum > res) res = sum;
  }
  cout << res << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    solve(cs);
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1063 Another Maximum Sum in Subarray
Contest
Brain Booster #4
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-14 18:03:58
Judged At
2024-11-11 03:22:29
Judged By
Score
5
Total Time
369ms
Peak Memory
824.0 KiB