結果

問題 No.1767 BLUE to RED
ユーザー bonKotsu
提出日時 2022-07-24 15:30:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 218 ms / 2,000 ms
コード長 1,098 bytes
コンパイル時間 4,021 ms
コンパイル使用メモリ 252,648 KB
最終ジャッジ日時 2025-01-30 13:42:19
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define LP pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
template<class T>
void chmax(T& a, T b) { a = max(a, b); };
template<class T>
void chmin(T& a, T b) { a = min(a, b); };

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> a(n+2), b(m);
  const ll INF = 1e18;
  a[0] = -INF;
  a[n+1] = INF;
  rep(i,n) cin >> a[i+1];
  rep(i,m) cin >> b[i];
  vector<ll> g[n+1];
  // b のグループ分け
  rep(i,m) {
    auto num = lower_bound(all(a), b[i]) - a.begin();
    num--;
    g[num].pb(b[i]);
  }
  auto f = [&](ll l ,ll r, vector<ll> v) {
    ll res = r-l;
    ll mx = v[0]-l;
    v.pb(r);
    rep(i,v.size()-1) chmax(mx,v[i+1]-v[i]);
    return res-mx;

  };
  ll ans = 0;
  rep(i,n+1) {
    if (g[i].size()) ans += f(a[i], a[i+1], g[i]);
  }
  cout << ans << endl;



  return 0;
}
0