結果
| 問題 | 
                            No.2603 Tone Correction
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2024-01-12 22:09:49 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 68 ms / 2,000 ms | 
| コード長 | 1,581 bytes | 
| コンパイル時間 | 2,265 ms | 
| コンパイル使用メモリ | 197,156 KB | 
| 最終ジャッジ日時 | 2025-02-18 18:22:31 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 33 | 
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
/*
Take adjacent differences
Then want to do some greedy?
Can do +1 or -1 on one of them.
need to make everything 0
can balance out +1's and -1's.
can do +M or -M free of charge.
either needs +1's or -1's
can try all those options of switchings?
*/
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m; cin >> n>> m;
    vi a(n),b(n);
    for(auto& i : a) cin >> i;
    for(auto& i : b) cin >> i;
    vi c(n);
    c[0]=(a[0]-b[0]+m)%m;
    for(int i=1;i<n;++i) {
        int cur = (a[i]-b[i]+m)%m;
        int prv = (a[i-1]-b[i-1]+m)%m;
        c[i] = (cur-prv+m)%m;
    }
    sort(all(c));
    ll minus = accumulate(all(c),0LL);
    ll plus=0;
    ll ans = minus;
    for(int i=n-1;i>=0;--i) {
        auto cur = c[i];
        if(cur) {
            minus-=c[i];
            plus+=m-c[i];
        }
        ans=min(ans,max(minus,plus));
    }
    cout << ans << '\n';
}