/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 332.0 KiB
#2 Wrong Answer 2ms 540.0 KiB
#3 Wrong Answer 401ms 816.0 KiB
#4 Time Exceeded ≥1043ms ≥3.121 MiB
#5 Time Exceeded ≥1039ms ≥8.539 MiB
#6 Wrong Answer 104ms 596.0 KiB
#7 Wrong Answer 314ms 1.672 MiB
#8 Wrong Answer 385ms 2.078 MiB
#9 Wrong Answer 576ms 2.613 MiB
#10 Wrong Answer 61ms 664.0 KiB
#11 Wrong Answer 24ms 584.0 KiB
#12 Time Exceeded ≥1009ms ≥31.859 MiB
#13 Time Exceeded ≥1043ms ≥32.156 MiB
#14 Time Exceeded ≥1046ms ≥31.812 MiB
#15 Time Exceeded ≥1024ms ≥72.062 MiB
#16 Time Exceeded ≥1073ms ≥37.68 MiB
#17 Time Exceeded ≥1017ms ≥18.461 MiB

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-10-03 13:35:30
Judged By
Score
5
Total Time
≥1073ms
Peak Memory
≥72.062 MiB