/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 328.0 KiB
#2 Wrong Answer 3ms 532.0 KiB
#3 Time Exceeded ≥1038ms ≥584.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];
  }
  int64_t res = 0;
  for (int i = 0; i <= n - k; i++) {
    multiset<int> S;
    for (int j = i; j < i + k; j++) {
      S.insert(a[j]);
    }
    vector<int> taken(n, 0);
    int64_t sum = 0;
    for (auto it : S) {
      pair<int, int> who = {it, i};
      for (int j = i - 1; ~j; --j) {
        if (!taken[j]) {
          if (__gcd(it, a[j]) > 1) {
            if (a[j] > who.first) {
              who.first = a[j];
              who.second = j;
            }
          }
        }
      }
      for (int j = i + 1; j < n; j++) {
        if (!taken[j]) {
          if (__gcd(it, a[j]) > 1) {
            if (a[j] > who.first) {
              who.first = a[j];
              who.second = j;
            }
          }
        }
      }
      sum += who.first;
      taken[who.second] = 1;
    }
    res = max(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 17:39:17
Judged At
2024-11-11 03:22:47
Judged By
Score
5
Total Time
≥1038ms
Peak Memory
≥584.0 KiB