Submission #6789081


Source Code Expand

Copy
#include <bits/stdc++.h>

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

signed main() {
  ll N, K, s=0;
  cin>>N>>K;
  vector<ll> a(N);
  rep(i,0,N){
    cin>>a[i];
    s+=a[i];
  }

  // 約数一覧
  set<ll> divisors;
  for (ll i=1; i*i<=s; i++) {
    if (s%i == 0) {
      divisors.insert(i);
      divisors.insert(s/i);
    }
  }

  ll ans = -1;
  for (auto d: divisors) {
    vector<ll> x(N);
    vector<ll> psum1(N+2, 0); // psum1[i]: i番目までのあまりの総和
    vector<ll> psum2(N+2, 0); // psum2[i]: i番目以降のあまりの総和
    rep(i,0,N){
      x[i] = a[i] % d;
    }
    sort(begin(x),end(x));

    rep(i,1,N+1){
      psum1[i] = psum1[i-1] + x[i-1];
    }
    for (int i=N; i>=1; i--){
      psum2[i] = psum2[i+1] + x[i-1];
    }
    rep(i,0,N+1){
      // [0 i]  を -1
      // [i+1 N+1]を1
      // if (d==7) {
      //   printf("psum1[%d]=%d\n", i,   psum1[i]);
      //   printf("psum2[%d]=%d\n", i+1, psum2[i+1]);
      //   printf("d*(%d) - psum2[%d]=%d\n", N-i, i+1, d*(N-i+1) - psum2[i+1]);
      //   cout<<endl;
      // }
      if (max(psum1[i], d*(N-i) - psum2[i+1]) <= K) {
        ans=max(ans,d);
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}

Submission Info

Submission Time
Task E - Max GCD
User bobuhiro11
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1360 Byte
Status
Exec Time 26 ms
Memory 256 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 s1.txt, s2.txt, s3.txt, s4.txt
All 500 / 500 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt, 67.txt, 68.txt, 69.txt, 70.txt, 71.txt, 72.txt, 73.txt, 74.txt, 75.txt, 76.txt, 77.txt, 78.txt, 79.txt, 80.txt, s1.txt, s2.txt, s3.txt, s4.txt
Case Name Status Exec Time Memory
01.txt 1 ms 256 KB
02.txt 3 ms 256 KB
03.txt 1 ms 256 KB
04.txt 1 ms 256 KB
05.txt 1 ms 256 KB
06.txt 1 ms 256 KB
07.txt 1 ms 256 KB
08.txt 1 ms 256 KB
09.txt 1 ms 256 KB
10.txt 1 ms 256 KB
11.txt 1 ms 256 KB
12.txt 1 ms 256 KB
13.txt 2 ms 256 KB
14.txt 2 ms 256 KB
15.txt 1 ms 256 KB
16.txt 1 ms 256 KB
17.txt 2 ms 256 KB
18.txt 2 ms 256 KB
19.txt 2 ms 256 KB
20.txt 2 ms 256 KB
21.txt 5 ms 256 KB
22.txt 4 ms 256 KB
23.txt 3 ms 256 KB
24.txt 2 ms 256 KB
25.txt 3 ms 256 KB
26.txt 2 ms 256 KB
27.txt 2 ms 256 KB
28.txt 3 ms 256 KB
29.txt 3 ms 256 KB
30.txt 6 ms 256 KB
31.txt 3 ms 256 KB
32.txt 3 ms 256 KB
33.txt 4 ms 256 KB
34.txt 4 ms 256 KB
35.txt 2 ms 256 KB
36.txt 4 ms 256 KB
37.txt 5 ms 256 KB
38.txt 2 ms 256 KB
39.txt 2 ms 256 KB
40.txt 3 ms 256 KB
41.txt 5 ms 256 KB
42.txt 4 ms 256 KB
43.txt 2 ms 256 KB
44.txt 3 ms 256 KB
45.txt 2 ms 256 KB
46.txt 2 ms 256 KB
47.txt 2 ms 256 KB
48.txt 2 ms 256 KB
49.txt 2 ms 256 KB
50.txt 2 ms 256 KB
51.txt 2 ms 256 KB
52.txt 2 ms 256 KB
53.txt 2 ms 256 KB
54.txt 2 ms 256 KB
55.txt 2 ms 256 KB
56.txt 2 ms 256 KB
57.txt 3 ms 256 KB
58.txt 2 ms 256 KB
59.txt 2 ms 256 KB
60.txt 4 ms 256 KB
61.txt 2 ms 256 KB
62.txt 2 ms 256 KB
63.txt 2 ms 256 KB
64.txt 2 ms 256 KB
65.txt 2 ms 256 KB
66.txt 2 ms 256 KB
67.txt 2 ms 256 KB
68.txt 3 ms 256 KB
69.txt 2 ms 256 KB
70.txt 2 ms 256 KB
71.txt 2 ms 256 KB
72.txt 2 ms 256 KB
73.txt 2 ms 256 KB
74.txt 2 ms 256 KB
75.txt 2 ms 256 KB
76.txt 25 ms 256 KB
77.txt 26 ms 256 KB
78.txt 17 ms 256 KB
79.txt 25 ms 256 KB
80.txt 23 ms 256 KB
s1.txt 1 ms 256 KB
s2.txt 1 ms 256 KB
s3.txt 1 ms 256 KB
s4.txt 1 ms 256 KB