結果

問題 No.1120 Strange Teacher
ユーザー yuji9511yuji9511
提出日時 2020-07-22 21:35:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 24 ms / 1,000 ms
コード長 1,489 bytes
コンパイル時間 1,926 ms
コンパイル使用メモリ 193,628 KB
最終ジャッジ日時 2025-01-12 02:17:57
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}

void solve(){
    ll N;
    cin >> N;
    ll a[100010], b[100010];
    rep(i,0,N) cin >> a[i];
    rep(i,0,N) cin >> b[i];
    ll sumA = 0, sumB = 0;
    rep(i,0,N) sumA += a[i];
    rep(i,0,N) sumB += b[i];
    ll diff = N-2;
    if(N == 2){
        if(sumA == sumB){
            print(abs(a[0] - b[0]));
        }else{
            print(-1);
        }
        return;
    }
    if(sumA - sumB < 0){
        print(-1);
    }else if((sumA - sumB) % diff != 0){
        print(-1);
    }else{
        ll ans = (sumA - sumB) / diff;
        bool ok = true;
        rep(i,0,N){
            a[i] -= ans; 
        }
        ll v = 0;
        rep(i,0,N){
            if(b[i] - a[i] < 0 || (b[i] - a[i]) % 2 == 1){
                ok = false;
            }else{
                v += (b[i] - a[i]) / 2;
            }
        }
        if(ans != v) ok = false;
        if(ok){
            print(ans);
        }else{
            print(-1);
        }

    }

    

}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
0