답안 #636991

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636991 2022-08-31T01:50:46 Z iee Simfonija (COCI19_simfonija) C++17
110 / 110
20 ms 3680 KB
// iee
#include <bits/stdc++.h>

#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define fi first
#define se second
using ll = long long;
using ull = unsigned long long;
using namespace std;
void work(int);

template <class T> void read(T &x) {
  x = 0;
  int f = 1, ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  x *= f;
}

int main() {
  int TT = 1; // cin >> TT;
  rep(CAS, 1, TT)
    work(CAS);
  return 0;
}
const int N = 1e5 + 5;
int n, k, a[N], b[N], d[N];
ll s[N];
ll calc(int l, int r) {
  int mid = l + (r - l + 1 + 1) / 2 - 1;
  return (ll) d[mid] * (mid - (l - 1)) - (s[mid] - s[l - 1]) + (s[r] - s[mid]) - (ll) d[mid] * (r - mid);
}
void work(int CASE) {
  read(n), read(k);
  k = n - k;
  rep(i, 1, n) read(a[i]);
  rep(i, 1, n) read(b[i]), d[i] = a[i] - b[i];
  sort(d + 1, d + n + 1);
  rep(i, 1, n) s[i] = s[i - 1] + d[i];
  ll mn = 1e18;
  rep(l, 1, n - k + 1) mn = min(mn, calc(l, l + k - 1));
  printf("%lld", mn);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2176 KB Output is correct
2 Correct 12 ms 2220 KB Output is correct
3 Correct 11 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 2132 KB Output is correct
2 Correct 12 ms 2256 KB Output is correct
3 Correct 11 ms 2200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 2260 KB Output is correct
2 Correct 14 ms 2260 KB Output is correct
3 Correct 11 ms 2132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 2260 KB Output is correct
2 Correct 11 ms 3616 KB Output is correct
3 Correct 13 ms 3680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 2132 KB Output is correct
2 Correct 15 ms 2188 KB Output is correct
3 Correct 15 ms 2132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2204 KB Output is correct
2 Correct 12 ms 2204 KB Output is correct
3 Correct 20 ms 3564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 2260 KB Output is correct
2 Correct 14 ms 3580 KB Output is correct
3 Correct 16 ms 3584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 2260 KB Output is correct
2 Correct 17 ms 2248 KB Output is correct
3 Correct 16 ms 3576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 2200 KB Output is correct
2 Correct 12 ms 2180 KB Output is correct
3 Correct 16 ms 2248 KB Output is correct