Problem:
F: A2B
問題
長さ $N$ の整数列 $A = (a_1, a_2, \dots, a_N)$, $B = (b_1, b_2, \dots, b_N)$ が与えられます。
あなたは数列 $A$ に対して以下の操作を $0$ 回以上行うことができます。
整数 $i$ ($1 < i < N$) を選び、$a_{i-1}, a_{i+1}$ に $a_i$ を加算し、その後 $a_i$ に $-1$ を掛ける。
操作を繰り返すことで、整数列 $A$ を整数列 $B$ に変換できるか判定し、可能な場合は必要な操作回数の最小値を答えてください。
入力形式
$N$
$a_1$ $a_2$ $\ldots$ $a_N$
$b_1$ $b_2$ $\ldots$ $b_N$
制約
$3 \leq N \leq 300{,}000$
$-10^{9} \leq a_i \leq 10^{9}$
$-10^{9} \leq b_i \leq 10^{9}$
出力形式
整数列 $A$ を整数列 $B$ に変換できるなら必要な操作回数の最小値を、不可能ならば $-1$ を一行に出力してください。
入力例 1
4
1 2 3 4
3 3 -5 9
出力例 1
2
$i=2, i=3$ に対して順に操作を行うと、数列は $(1, 2, 3, 4) \rightarrow (3, -2, 5, 4) \rightarrow (3, 3, -5, 9)$ と変化します。
このような $2$ 回の操作によって $A$ を $B$ に等しくでき、また $2$ 回未満の操作では $A$ を $B$ に等しくできないので、最小の操作回数である $2$ を出力してください。
入力例 2
3
1 2 3
3 2 1
出力例 2
-1
どのように操作しても $A$ を $B$ に等しくできないので、 $-1$ を出力してください。

Submitted solutions:

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
////////////////////////////////////////////////
template <class T>
struct BIT
{

public:
    BIT() : _n(0) {}
    explicit BIT(int n) : _n(n), data(n) {}

    // サイズを与えて初期化
    void init(int n)
    {
        _n = n;
        data.resize(_n);
    }

    // 配列を与えて初期化
    void init(vector<T> ar)
    {
        _n = ar.size();
        data.resize(_n);
        rep(i, 0, ar.size())
        {
            add(i, ar[i]);
        }
    }

    // p番目にxを足す
    void add(int p, T x)
    {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n)
        {
            data[p - 1] += x;
            p += p & -p;
        }
    }

    // p番目をxにする
    void set(int p, T x)
    {
        T y = sum(p, p + 1);
        add(p, -y);
        add(p, x);
    }

    //[l,r)の和
    T sum(int l, int r)
    {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

private:
    int _n;
    std::vector<T> data;

    T sum(int r)
    {
        T s = 0;
        while (r > 0)
        {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};
//////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, 0, n)
    {
        cin >> a[i];
    }
    rep(i, 0, n)
    {
        cin >> b[i];
    }
    rep(i, 0, n - 1)
    {
        a[i + 1] += a[i];
        b[i + 1] += b[i];
    }
    if (a[n - 1] != b[n - 1])
    {
        cout << -1 << endl;
        return 0;
    }
    vector<ll> c = a, d = b;
    srt(c);
    srt(d);
    if (c != d)
    {
        cout << -1 << Endl;
        return 0;
    }
    map<ll, ll> mp;
    set<ll> st;
    rep(i, 0, n)
    {
        st.insert(a[i]);
    }
    ll cnt = 0;
    for (auto i : st)
    {
        mp[i] = cnt;
        cnt++;
    }
    rep(i, 0, n)
    {
        a[i] = mp[a[i]];
        b[i] = mp[b[i]];
    }
    vector<queue<ll>> ar(n);
    rep(i, 0, n)
    {
        ar[b[i]].push(i);
    }
    rep(i, 0, n)
    {
        ll x = ar[a[i]].front();
        ar[a[i]].pop();
        a[i] = x;
    }
    BIT<ll> bit(n);
    ll ans = 0;
    rep(i, 0, n)
    {
        ans += bit.sum(a[i] + 1, n);
        bit.add(a[i], 1);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 480
memory_kb: 249424
final_rank: 7

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
////////////////////////////////////////////////
template <class T>
struct BIT
{

public:
    BIT() : _n(0) {}
    explicit BIT(int n) : _n(n), data(n) {}

    // サイズを与えて初期化
    void init(int n)
    {
        _n = n;
        data.resize(_n);
    }

    // 配列を与えて初期化
    void init(vector<T> ar)
    {
        _n = ar.size();
        data.resize(_n);
        rep(i, 0, ar.size())
        {
            add(i, ar[i]);
        }
    }

    // p番目にxを足す
    void add(int p, T x)
    {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n)
        {
            data[p - 1] += x;
            p += p & -p;
        }
    }

    // p番目をxにする
    void set(int p, T x)
    {
        T y = sum(p, p + 1);
        add(p, -y);
        add(p, x);
    }

    //[l,r)の和
    T sum(int l, int r)
    {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

private:
    int _n;
    std::vector<T> data;

    T sum(int r)
    {
        T s = 0;
        while (r > 0)
        {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};
//////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, 0, n)
    {
        cin >> a[i];
    }
    rep(i, 0, n)
    {
        cin >> b[i];
    }
    rep(i, 0, n - 1)
    {
        a[i + 1] += a[i];
        b[i + 1] += b[i];
    }
    vector<ll> c = a, d = b;
    srt(c);
    srt(d);
    if (c != d)
    {
        cout << -1 << Endl;
        return 0;
    }
    map<ll, queue<ll>> mp;
    rep(i, 0, n)
    {
        mp[b[i]].push(i);
    }
    rep(i, 0, n)
    {
        ll x = mp[a[i]].front();
        mp[a[i]].pop();
        a[i] = x;
    }
    BIT<ll> bit(n);
    ll ans = 0;
    rep(i, 0, n)
    {
        ans += bit.sum(a[i] + 1, n);
        bit.add(a[i], 1);
    }
    cout << ans << endl;
}
accuracy: 0.8297872340425532
time_ms: 470
memory_kb: 230668
final_rank: 11

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
////////////////////////////////////////////////
template <class T>
struct BIT
{

public:
    BIT() : _n(0) {}
    explicit BIT(int n) : _n(n), data(n) {}

    // サイズを与えて初期化
    void init(int n)
    {
        _n = n;
        data.resize(_n);
    }

    // 配列を与えて初期化
    void init(vector<T> ar)
    {
        _n = ar.size();
        data.resize(_n);
        rep(i, 0, ar.size())
        {
            add(i, ar[i]);
        }
    }

    // p番目にxを足す
    void add(int p, T x)
    {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n)
        {
            data[p - 1] += x;
            p += p & -p;
        }
    }

    // p番目をxにする
    void set(int p, T x)
    {
        T y = sum(p, p + 1);
        add(p, -y);
        add(p, x);
    }

    //[l,r)の和
    T sum(int l, int r)
    {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

private:
    int _n;
    std::vector<T> data;

    T sum(int r)
    {
        T s = 0;
        while (r > 0)
        {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};
//////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, 0, n)
    {
        cin >> a[i];
    }
    rep(i, 0, n)
    {
        cin >> b[i];
    }
    rep(i, 0, n - 1)
    {
        a[i + 1] += a[i];
        b[i + 1] += b[i];
    }
    vector<ll> c = a, d = b;
    srt(c);
    srt(d);
    if (c != d)
    {
        cout << -1 << Endl;
        return 0;
    }
    a.resize(n - 1);
    b.resize(n - 1);
    n--;
    map<ll, ll> mp;
    rep(i, 0, n)
    {
        if (mp.find(b[i]) == mp.end())
        {
            mp[b[i]] = i;
        }
    }
    rep(i, 0, n)
    {
        a[i] = mp[a[i]];
    }
    BIT<ll> bit(n);
    ll ans = 0;
    rep(i, 0, n)
    {
        ans += bit.sum(a[i] + 1, n);
        bit.add(a[i], 1);
    }
    cout << ans << endl;
}
accuracy: 0.6170212765957447
time_ms: 220
memory_kb: 33800
final_rank: 17

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}
//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
////////////////////////////////////////////////
template <class T>
struct BIT
{

public:
    BIT() : _n(0) {}
    explicit BIT(int n) : _n(n), data(n) {}

    // サイズを与えて初期化
    void init(int n)
    {
        _n = n;
        data.resize(_n);
    }

    // 配列を与えて初期化
    void init(vector<T> ar)
    {
        _n = ar.size();
        data.resize(_n);
        rep(i, 0, ar.size())
        {
            add(i, ar[i]);
        }
    }

    // p番目にxを足す
    void add(int p, T x)
    {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n)
        {
            data[p - 1] += x;
            p += p & -p;
        }
    }

    // p番目をxにする
    void set(int p, T x)
    {
        T y = sum(p, p + 1);
        add(p, -y);
        add(p, x);
    }

    //[l,r)の和
    T sum(int l, int r)
    {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

private:
    int _n;
    std::vector<T> data;

    T sum(int r)
    {
        T s = 0;
        while (r > 0)
        {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};
//////////////////////////////////////////
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, 0, n)
    {
        cin >> a[i];
    }
    rep(i, 0, n)
    {
        cin >> b[i];
    }
    rep(i, 0, n - 1)
    {
        a[i + 1] += a[i];
        b[i + 1] += b[i];
    }
    vector<ll> c = a, d = b;
    srt(c);
    srt(d);
    if (c != d)
    {
        cout << -1 << Endl;
        return 0;
    }
    a.resize(n - 1);
    b.resize(n - 1);
    n--;
    map<ll, ll> mp;
    rep(i, 0, n)
    {
        mp[b[i]] = i;
    }
    rep(i, 0, n)
    {
        a[i] = mp[a[i]];
    }
    BIT<ll> bit(n);
    ll ans = 0;
    rep(i, 0, n)
    {
        ans += bit.sum(a[i] + 1, n);
        bit.add(a[i], 1);
    }
    cout << ans << endl;
}
accuracy: 0.6170212765957447
time_ms: 200
memory_kb: 33800
final_rank: 16

code_snippet: #ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ～ 2^63 = 9e18（int は -2^31 ～ 2^31 = 2e9）
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍（下，右，上，左）
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素（変更不可能）
#define repea(v, a) for(auto& v : (a)) // a の全要素（変更可能）
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索（昇順）
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素（昇順）
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て（昇順）
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定

// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新（更新されたら true を返す）
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新（更新されたら true を返す）
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


//#if __has_include(<atcoder/all>)
//#include <atcoder/all>
//using namespace atcoder;
//
//#ifdef _MSC_VER
//#include "localACL.hpp"
//#endif
//
//using mint = modint998244353;
////using mint = static_modint<1000000007>;
////using mint = modint; // mint::set_mod(m);
//
//namespace atcoder {
//	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
//	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
//}
//using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
//#endif


#ifdef _MSC_VER // 手元環境（Visual Studio）
#include "local.hpp"
#else // 提出用（gcc）
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(...)
#define dump_math(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif


//【フェニック木（ACL 仕様）】
/*
* fenwick_tree(n) : O(n)
*	要素数 n かつ初期値 0 で初期化する．
*
* add(p, val) : O(log n)
*	v[p] に val を加算する．
*
* sum(l, r) : O(log n)
*	半開区間 [l, r) の要素の総和を返す．
*/
template <class T>
struct fenwick_tree {
	// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/all/CGL_6_A

	int n;
	vector<T> v;

	fenwick_tree() {}
	fenwick_tree(int n_) : n(n_ + 1), v(n) {}
	void add(int p, T val) {
		p++;
		while (p < n) {
			v[p] += val;
			p += p & -p;
		}
	}
	T sum_sub(int r) const {
		T res = 0;
		while (r > 0) {
			res += v[r];
			r -= r & -r;
		}
		return res;
	}
	T sum(int l, int r) const {
		return sum_sub(r) - sum_sub(l);
	}

#ifdef _MSC_VER
	friend ostream& operator<<(ostream& os, const fenwick_tree& ft) {
		rep(i, ft.n - 1) {
			os << ft.sum(i, i + 1) << " ";
		}
		return os;
	}
#endif
};


//【転倒数（座圧）】O(n log n)
/*
* a[0..n) の転倒数を返す．
*/
template <class T>
ll inversion_number_cc(const vector<T>& a) {
	// verify : https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_5_D

	int n = sz(a);

	vector<T> a_uniq(a);
	uniq(a_uniq);

	int m = sz(a_uniq);

	// fw[i] : 今まで見てきた範囲に値 i が何個あったか
	fenwick_tree<int> fw(m);
	ll res = 0;

	// 位置について昇順に見ていく．
	rep(i, n) {
		int b = lbpos(a_uniq, a[i]);
		dump(b);

		fw.add(b, 1);

		// 自身より大きい数が今までに何個あったか調べ，加算する．
		res += fw.sum(b + 1, m);

		dump(fw);
	}

	return res;
}


//【最小 swap 回数】O(n log n)
/*
* 数列 a[0..n) に対し，隣接要素の swap を繰り返すことで b[0..n) に変化させるときの
* swap の最小回数を返す（不可能なら INFL）
*
* 利用：【転倒数】
*/
template <class T>
ll swap_distance(const vector<T>& a, const vector<T>& b) {
	// verify : https://atcoder.jp/contests/arc120/tasks/arc120_c

	int n = sz(a);

	// 長さが異なるならもちろん不可能
	if (sz(b) != n) return INFL;

	vector<pair<T, int>> ai(n), bi(n);
	rep(i, n) {
		ai[i] = { a[i], i };
		bi[i] = { b[i], i };
	}

	// 安定ソートになるので，左同士，右同士を対応付けてくれる．
	sort(all(ai));
	sort(all(bi));

	// p[ia] = ib : a[ia] を b[ib] と対応させることを表す．
	vi p(n);
	rep(i, n) {
		auto& [a, ia] = ai[i];
		auto& [b, ib] = bi[i];
		if (a != b) return INFL;

		p[ia] = ib;
	}

	// 順列 p[0..n) の転倒数が答え．
	return inversion_number_cc(p);
}


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

	int n;
	cin >> n;

	vl a(n), b(n);
	cin >> a >> b;

	repi(i, 1, n - 1) {
		a[i] += a[i - 1];
		b[i] += b[i - 1];
	}

	if (a[n - 1] != b[n - 1]) EXIT(-1);

	a.pop_back();
	b.pop_back();

	dump(a); dump(b);

	ll res = swap_distance(a, b);
	if (res == INFL) res = -1;

	EXIT(res);
}
accuracy: 1
time_ms: 120
memory_kb: 20596
final_rank: 3

code_snippet: #include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}


template<int mod>
struct ModInt{
	int x;
	ModInt () : x(0) {}
	ModInt (int64_t x) : x(x >= 0 ? x % mod : (mod - -x % mod) % mod) {}
	ModInt &operator += (const ModInt &p){
		if ((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator -= (const ModInt &p) {
		if ((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator *= (const ModInt &p) {
		x = (int64_t) x * p.x % mod;
		return *this;
	}
	ModInt &operator /= (const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt &operator ^= (int64_t p) {
		ModInt res = 1;
		for (; p; p >>= 1) {
			if (p & 1) res *= *this;
			*this *= *this;
		}
		return *this = res;
	}
	ModInt operator - () const { return ModInt(-x); }
	ModInt operator + (const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator - (const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator * (const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator / (const ModInt &p) const { return ModInt(*this) /= p; }
	ModInt operator ^ (int64_t p) const { return ModInt(*this) ^= p; }
	bool operator == (const ModInt &p) const { return x == p.x; }
	bool operator != (const ModInt &p) const { return x != p.x; }
	explicit operator int() const { return x; }
	ModInt &operator = (const int p) { x = p; return *this;}
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			a -= t * b;
			std::swap(a, b);
			u -= t * v;
			std::swap(u, v);
		}
		return ModInt(u);
	}
	friend std::ostream & operator << (std::ostream &stream, const ModInt<mod> &p) {
		return stream << p.x;
	}
	friend std::istream & operator >> (std::istream &stream, ModInt<mod> &a) {
		int64_t x;
		stream >> x;
		a = ModInt<mod>(x);
		return stream;
	}
};
typedef ModInt<998244353> mint;

template<int mod> struct MComb {
	using mint = ModInt<mod>;
	std::vector<mint> fact;
	std::vector<mint> inv;
	MComb (int n) { // O(n + log(mod))
		fact = std::vector<mint>(n + 1, 1);
		for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * mint(i);
		inv.resize(n + 1);
		inv[n] = fact[n] ^ (mod - 2);
		for (int i = n; i--; ) inv[i] = inv[i + 1] * mint(i + 1);
	}
	mint ncr(int n, int r) {
		return fact[n] * inv[r] * inv[n - r];
	}
	mint npr(int n, int r) {
		return fact[n] * inv[n - r];
	}
	mint nhr(int n, int r) {
		assert(n + r - 1 < (int) fact.size());
		return ncr(n + r - 1, r);
	}
};
struct BIT {
	std::vector<int> data;
	int n;
	BIT(int n) {
		this->n = n;
		data.resize(n + 1);
	}
	void add(int i, int val) {
		for (i++; i <= n; i += i & -i) data[i] += val;
	}
	int sum(int i) {
		int res = 0;
		for (; i; i -= i & -i) res += data[i];
		return res;
	}
};

int64_t solve(const std::vector<int64_t> &a, const std::vector<int64_t> &b) {
	int n = a.size();
	
	std::map<int64_t, int> cnt;
	for (auto i : a) cnt[i]++;
	for (auto i : b) cnt[i]--;
	for (auto i : cnt) if (i.second) return -1;
	
	std::map<int64_t, std::queue<int> > index;
	for (int i = 0; i < n; i++) index[a[i]].push(i);
	std::vector<int> perm(n);
	for (int i = 0; i < n; i++) perm[i] = index[b[i]].front(), index[b[i]].pop();
	
	BIT tree(n); 
	int64_t res = 0;
	for (int i = 0; i < n; i++) res += tree.sum(perm[i]), tree.add(perm[i], 1);
	res = (int64_t) n * (n - 1) / 2 - res;
	return res;
}

int main() {
	int n = ri();
	int a[n];
	int b[n];
	for (auto &i : a) i = ri();
	for (auto &i : b) i = ri();
	std::vector<int64_t> sa(n + 1);
	std::vector<int64_t> sb(n + 1);
	for (int i = 0; i < n; i++) sa[i + 1] = sa[i] + a[i];
	for (int i = 0; i < n; i++) sb[i + 1] = sb[i] + b[i];
	
	if (sa.back() != sb.back()) puts("-1");
	else {
		sa.erase(sa.begin());
		sa.pop_back();
		sb.erase(sb.begin());
		sb.pop_back();
		printf("%" PRId64 "\n", solve(sa, sb));
	}
	
	return 0;
}
accuracy: 1
time_ms: 740
memory_kb: 246656
final_rank: 9

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long long solve(int N, vector<long long> A, vector<long long> B) {
	vector<int> pa(N), pb(N);
	for (int i = 0; i < N; ++i) {
		pa[i] = i;
		pb[i] = i;
	}
	sort(pa.begin(), pa.end(), [&](int i, int j) { return A[i] != A[j] ? A[i] < A[j] : i < j; });
	sort(pb.begin(), pb.end(), [&](int i, int j) { return B[i] != B[j] ? B[i] < B[j] : i < j; });
	for (int i = 0; i < N; ++i) {
		if (A[pa[i]] != B[pb[i]]) {
			return -1;
		}
	}
	vector<int> ipa(N), ipb(N);
	for (int i = 0; i < N; ++i) {
		ipa[pa[i]] = i;
		ipb[pb[i]] = i;
	}
	vector<int> p(N);
	for (int i = 0; i < N; ++i) {
		p[i] = pa[ipb[i]];
	}
	reverse(p.begin(), p.end());
	long long ans = 0;
	vector<int> bit(N + 1);
	for (int i : p) {
		for (int j = i; j >= 1; j -= j & (-j)) {
			ans += bit[j];
		}
		for (int j = i + 1; j <= N; j += j & (-j)) {
			++bit[j];
		}
	}
	return ans;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<long long> A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> B[i];
	}
	vector<long long> SA(N + 1), SB(N + 1);
	for (int i = 0; i < N; i++) {
		SA[i + 1] = SA[i] + A[i];
	}
	for (int i = 0; i < N; i++) {
		SB[i + 1] = SB[i] + B[i];
	}
	cout << solve(N - 1, vector<long long>(SA.begin() + 1, SA.begin() + N), vector<long long>(SB.begin() + 1, SB.begin() + N)) << endl;
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 24140
final_rank: 2

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long long solve(int N, vector<long long> A, vector<long long> B) {
	vector<int> pa(N), pb(N);
	for (int i = 0; i < N; ++i) {
		pa[i] = i;
		pb[i] = i;
	}
	sort(pa.begin(), pa.end(), [&](int i, int j) { return A[i] != A[j] ? A[i] < A[j] : i < j; });
	sort(pb.begin(), pb.end(), [&](int i, int j) { return B[i] != B[j] ? B[i] < B[j] : i < j; });
	for (int i = 0; i < N; ++i) {
		if (A[pa[i]] != B[pb[i]]) {
			return -1;
		}
	}
	vector<int> ipa(N), ipb(N);
	for (int i = 0; i < N; ++i) {
		ipa[pa[i]] = i;
		ipb[pb[i]] = i;
	}
	vector<int> p(N);
	for (int i = 0; i < N; ++i) {
		p[i] = pa[ipb[i]];
	}
	reverse(p.begin(), p.end());
	long long ans = 0;
	vector<int> bit(N + 1);
	for (int i : p) {
		for (int j = i; j >= 1; j -= j & (-j)) {
			ans += bit[j];
		}
		for (int j = i + 1; j <= N; j += j & (-j)) {
			++bit[j];
		}
	}
	return ans;
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<long long> A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> B[i];
	}
	vector<long long> SA(N + 1), SB(N + 1);
	for (int i = 0; i < N; i++) {
		SA[i + 1] = SA[i] + A[i];
	}
	for (int i = 0; i < N; i++) {
		SB[i + 1] = SB[i] + B[i];
	}
	cout << solve(N + 1, SA, SB) << endl;
	return 0;
}
accuracy: 0.8297872340425532
time_ms: 90
memory_kb: 24204
final_rank: 10

code_snippet: #include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <queue>
#include <string>


bool can_make(std::vector<long long int> from, std::vector<long long int> to) {
	std::sort(from.begin(), std::prev(from.end()));
	std::sort(to.begin(), std::prev(to.end()));
	for (auto i = 0; i < from.size(); ++i) {
		if (from[i] != to[i]) return false;
	}
	return true;
}
class Bit {
	std::vector<int> vec;
public:
	Bit(const int size) : vec(size, 0) {};
	int get(int position) const {
		int result = 0;
		while (position >= 0) {
			result += vec[position];
			position -= ~position & (position + 1);
		}
		return result;
	}
	void add(int position, const int value) {
		while (position < vec.size()) {
			vec[position] += value;
			position += ~position & (position + 1);
		}
	}
};
int main() {
	int n; std::cin >> n;
	std::vector<long long int> from(n), to(n);
	for (auto& f : from) {
		std::cin >> f;
	}
	for (auto& t : to) {
		std::cin >> t;
	}
	std::vector<long long int> from_sum{from}, to_sum{to};
	for (auto i = 1; i < n; ++i) {
		from_sum[i] += from_sum[i - 1];
		to_sum[i] += to_sum[i - 1];
	}
	if (!can_make(from_sum, to_sum)) {
		std::cout << "-1\n";
		return 0;
	}
	std::unordered_map<long long int, std::queue<int>> destination;
	for (auto i = 0; i < n; ++i) {
		destination[to_sum[i]].push(i);
	}
	std::vector<int> rank(n);
	for (auto i = 0; i < n; ++i) {
		auto& dest = destination[from_sum[i]];
		const auto position = dest.front(); dest.pop();
		rank[i] = position;
	}
	Bit bit(n);
	long long int result = 0;
	for (auto i = 0; i < n; ++i) {
		result += i - bit.get(rank[i]);
		bit.add(rank[i], 1);
	}
	std::cout << result << '\n';
}
accuracy: 1
time_ms: 450
memory_kb: 229264
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n-1);i>=0;i--)

using ll=long long;
struct BIT{
  int n;
  vector<int> bit;
  BIT(int n_):n(n_+1),bit(n,0){}
  void add(int idx,int a=1){
    for(int x=idx;x<n;x+=(x&-x))bit[x]+=a;
  }
  int sum(int idx){
    int res=0;
    for(int x=idx;x;x-=(x&-x))res+=bit[x];
    return res;
  }
};
map<ll,stack<int>> mp;
ll ans;

int main(){
  int n;cin>>n;
  vector<ll> v(n),w(n);
  REP(i,n)cin>>v[i];REP(i,n-1)v[i+1]+=v[i];
  REP(i,n)cin>>w[i];REP(i,n-1)w[i+1]+=w[i];
  if(v.back()!=w.back()){
      cout<<-1<<endl;
      return 0;
  }
  REP(i,n)mp[v[i]].push(i+1);
  BIT bit(n+5);
  RREP(i,n){
    if(mp[w[i]].size()){
      int r=mp[w[i]].top();mp[w[i]].pop();
      ans+=bit.sum(r);
      bit.add(r);
    }
    else{
      cout<<-1<<endl;
      return 0;
    }
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 570
memory_kb: 224604
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ti3 tuple<int,int,int>
#define mat vector<vector<ll>>
const int inf = 1 << 30;
const ll mod = 1e9 + 7;
const ll linf = 1e18;
const db EPS = 1e-10;
const db pi = acos(-1);
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

template<class T>
struct BIT{
    int N;
    vector<T> bit;
    BIT(int n = 0) : N(n), bit(n + 1, 0) {}
    T sum(int i){
        T s = 0;
        while(i > 0){
            s += bit[i];
            i -= i & -i;
        }
        return s;
    }

    void add(int i, T x){
        while(i <= N){
            bit[i] += x;
            i += i & -i;
        }
    }
    T sum(int l, int r){
        return sum(r) - sum(l - 1);
    }
};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int N;
    cin >> N;
    vector<ll> a(N), b(N), A(N), B(N);
    for (int i = 0; i < N; i++) cin >> a[i];
    for (int i = 0; i < N; i++) cin >> b[i];
    for (int i = 1; i < N; i++) a[i] += a[i - 1];
    for (int i = 1; i < N; i++) b[i] += b[i - 1];
    A = a, B = b;
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    for (int i = 0; i < N; i++) {
        if (A[i] != B[i] || a[N - 1] != b[N - 1]) {
            cout << -1 << endl;
            return 0;
        }
    }

    map<ll, queue<int>> mp;
    for (int i = 0; i < N; i++) {
        mp[a[i]].push(i + 1);
    }
    
    ll ans = 0;
    BIT<ll> bit(N + 1);
    for (int i = 0; i < N; i++) {
        int idx = mp[b[i]].front();
        mp[b[i]].pop();
        ans += bit.sum(idx, N);
        bit.add(idx, 1);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 530
memory_kb: 230592
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ti3 tuple<int,int,int>
#define mat vector<vector<ll>>
const int inf = 1 << 30;
const ll mod = 1e9 + 7;
const ll linf = 1e18;
const db EPS = 1e-10;
const db pi = acos(-1);
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

template<class T>
struct BIT{
    int N;
    vector<T> bit;
    BIT(int n = 0) : N(n), bit(n + 1, 0) {}
    T sum(int i){
        T s = 0;
        while(i > 0){
            s += bit[i];
            i -= i & -i;
        }
        return s;
    }

    void add(int i, T x){
        while(i <= N){
            bit[i] += x;
            i += i & -i;
        }
    }
    T sum(int l, int r){
        return sum(r) - sum(l - 1);
    }
};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int N;
    cin >> N;
    vector<ll> a(N), b(N), A(N), B(N);
    for (int i = 0; i < N; i++) cin >> a[i];
    for (int i = 0; i < N; i++) cin >> b[i];
    for (int i = 1; i < N; i++) a[i] += a[i - 1];
    for (int i = 1; i < N; i++) b[i] += b[i - 1];
    A = a, B = b;
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    for (int i = 0; i < N; i++) {
        if (A[i] != B[i]) {
            cout << -1 << endl;
            return 0;
        }
    }

    map<ll, queue<int>> mp;
    for (int i = 0; i < N; i++) {
        mp[a[i]].push(i + 1);
    }
    
    ll ans = 0;
    BIT<ll> bit(N + 1);
    for (int i = 0; i < N; i++) {
        int idx = mp[b[i]].front();
        mp[b[i]].pop();
        ans += bit.sum(idx, N);
        bit.add(idx, 1);
    }
    cout << ans << endl;
}
accuracy: 0.8297872340425532
time_ms: 530
memory_kb: 230512
final_rank: 12

code_snippet: #line 1 "main.cpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) ((int)(v).size())
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

#line 1 "/home/siro53/kyo-pro/compro_library/data_structure/BIT.hpp"
template <typename T> struct BIT {
    int n;
    vector<T> bit;
    BIT(){}
    BIT(int _n) { init(_n); }
    void init(int _n) {
        n = _n + 1;
        bit.resize(n + 1, 0);
    }
    // [0, k)
    T sum(int k) {
        T res = 0;
        for(int i = k - 1; i >= 0; i = (i & (i + 1)) - 1) { res += bit[i]; }
        return res;
    }
    // [l, r)
    T sum(int l, int r) { return (l < r ? sum(r) - sum(l) : 0); }
    // bit[k] += x
    void add(int k, T x) {
        for(int i = k; i < n; i |= i + 1) { bit[i] += x; }
    }
    // v[0] + ... + v[res] >= x
    int lower_bound(T x) {
        int res = -1;
        int k = 1;
        while(2 * k <= n) { k <<= 1; }
        for(; k > 0; k >>= 1) {
            if(res + k < n && bit[res + k] < x) {
                x -= bit[res + k];
                res += k;
            }
        }
        return res + 1;
    }
};
#line 77 "main.cpp"

int main() {
    int N;
    cin >> N;
    vector<ll> a(N), b(N);
    REP(i, N) cin >> a[i];
    REP(i, N) cin >> b[i];
    REP(i, N-1) a[i+1] += a[i];
    REP(i, N-1) b[i+1] += b[i];

    if(a.back() != b.back()) {
        cout << "-1\n";
        return 0;
    }

    vector<int> ord_a(N); iota(ALL(ord_a), 0);
    sort(ALL(ord_a), [&](int l, int r) {
        if(a[l] != a[r]) return a[l] < a[r];
        return l < r;
    });
    
    vector<int> ord_b(N); iota(ALL(ord_b), 0);
    sort(ALL(ord_b), [&](int l, int r) {
        if(b[l] != b[r]) return b[l] < b[r];
        return l < r;
    });

    vector<int> v(N);
    REP(i, N) {
        if(a[ord_a[i]] != b[ord_b[i]]) {
            cout << "-1\n";
            return 0;
        }
        v[ord_b[i]] = ord_a[i];
    }

    BIT<ll> fwt(N);
    ll ans = 0;
    REP(i, N) {
        ans += fwt.sum(v[i]+1, N);
        fwt.add(v[i], 1);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 90
memory_kb: 13572
final_rank: 1

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n-1);i>=0;i--)

using ll=long long;
struct BIT{
  int n;
  vector<int> bit;
  BIT(int n_):n(n_+1),bit(n,0){}
  void add(int idx,int a=1){
    for(int x=idx;x<n;x+=(x&-x))bit[x]+=a;
  }
  int sum(int idx){
    int res=0;
    for(int x=idx;x;x-=(x&-x))res+=bit[x];
    return res;
  }
};
map<ll,stack<int>> mp;
ll ans;

int main(){
  int n;cin>>n;
  vector<ll> v(n),w(n);
  REP(i,n)cin>>v[i];REP(i,n-1)v[i+1]+=v[i];
  REP(i,n)cin>>w[i];REP(i,n-1)w[i+1]+=w[i];
  REP(i,n)mp[v[i]].push(i+1);
  BIT bit(n+5);
  RREP(i,n){
    if(mp[w[i]].size()){
      int r=mp[w[i]].top();mp[w[i]].pop();
      ans+=bit.sum(r);
      bit.add(r);
    }
    else{
      cout<<-1<<endl;
      return 0;
    }
  }
  cout<<ans<<endl;
}
accuracy: 0.8297872340425532
time_ms: 570
memory_kb: 224612
final_rank: 14

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n-1);i>=0;i--)
#define ALL(v) v.begin(),v.end()

using ll=long long;
struct BIT{
  int n;
  vector<int> bit;
  BIT(int n_):n(n_+1),bit(n,0){}
  void add(int idx,int a=1){
    for(int x=idx;x<n;x+=(x&-x))bit[x]+=a;
  }
  int sum(int idx){
    int res=0;
    for(int x=idx;x;x-=(x&-x))res+=bit[x];
    return res;
  }
};

signed main(){
  int n;cin>>n;
  vector<ll> v(n),w(n);
  REP(i,n)cin>>v[i];
  REP(i,n)cin>>w[i];
  REP(i,n-1)v[i+1]+=v[i];
  REP(i,n-1)w[i+1]+=w[i];
  map<ll,stack<int>> mp;
  REP(i,n)mp[v[i]].push(i+1);
  ll ans=0;
  BIT bit(n+5);
  RREP(i,n){
    if(mp[w[i]].size()){
      int r=mp[w[i]].top();mp[w[i]].pop();
      ans+=bit.sum(r);
      bit.add(r);
    }
    else{
      cout<<-1<<endl;
      return 0;
    }
  }
  cout<<ans<<endl;
}
accuracy: 0.8297872340425532
time_ms: 570
memory_kb: 225864
final_rank: 15

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n-1);i>=0;i--)
#define ALL(v) v.begin(),v.end()

using ll=long long;
struct BIT{
  int n;
  vector<int> bit;
  BIT(int n_):n(n_+1),bit(n,0){}
  void add(int idx,int a=1){
    for(int x=idx;x<n;x+=(x&-x))bit[x]+=a;
  }
  int sum(int i){
    int res=0;
    for(int x=i;x;x-=(x&-x))res+=bit[x];
    return res;
  }
};

int main(){
  int n;cin>>n;
  vector<ll> v(n),w(n);
  REP(i,n)cin>>v[i];
  REP(i,n)cin>>w[i];
  REP(i,n-1)v[i+1]+=v[i];
  REP(i,n-1)w[i+1]+=w[i];
  map<ll,stack<int>> mp;
  REP(i,n)mp[v[i]].push(i+1);
  ll ans=0;
  BIT bit(n+5);
  RREP(i,n){
    if(mp[w[i]].size()){
      int r=mp[w[i]].top();mp[w[i]].pop();
      ans+=bit.sum(r);
      bit.add(r);
    }
    else{
      cout<<-1<<endl;
      return 0;
    }
  }
  cout<<ans<<endl;
}
accuracy: 0.8297872340425532
time_ms: 570
memory_kb: 224528
final_rank: 13

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n; cin >> n;
  vector<ll> a(n), b(n);
  REP(i,n) cin >> a[i];
  REP(i,n) cin >> b[i];
  ll suma = accumulate(all(a), 0LL);
  ll sumb = accumulate(all(b), 0LL);
  if(suma != sumb) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> ca(n+1), cb(n+1);
  REP(i,n) ca[i+1] = ca[i] + a[i];
  REP(i,n) cb[i+1] = cb[i] + b[i];

  map<ll,vector<int>> arr;
  for(int i=1; i<n; i++) arr[cb[i]].push_back(i);
  for(auto &e: arr) reverse(all(e.second));

  vector<int> t;
  for(int i=1; i<n; i++) {
    if(arr[ca[i]].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    t.push_back(arr[ca[i]].back());
    arr[ca[i]].pop_back();
  }
  BIT<ll> bit(n+1);
  ll ans = 0;
  for(int i=0; i<t.size(); i++) {
    ans += bit.rangesum(t[i], n+1);
    bit.add(t[i], 1);
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 48880
final_rank: 4

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n; cin >> n;
  vector<ll> a(n), b(n);
  REP(i,n) cin >> a[i];
  REP(i,n) cin >> b[i];
  ll suma = accumulate(all(a), 0LL);
  ll sumb = accumulate(all(b), 0LL);
  if(suma != sumb) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> ca(n+1), cb(n+1);
  REP(i,n) ca[i+1] = ca[i] + a[i];
  REP(i,n) cb[i+1] = cb[i] + b[i];

  {
    map<ll,int> cnt1, cnt2;
    for(int i=1; i<n; i++) {
      cnt1[ca[i]]++;
      cnt2[cb[i]]++;
    }
    for(auto e: cnt1) {
      if(e.second != cnt2[e.first]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  map<ll, int> mp;
  int t = 1;
  for(int i=1; i<=n; i++) { 
    mp[cb[i]] = t++;
  }
  BIT<ll> bit(t+1);
  ll ans = 0;
  for(int i=1; i<n; i++) {
    if(mp.find(ca[i]) == mp.end()) {
      cout << -1 << endl;
      return 0;
    }
    ca[i] = mp[ca[i]];
    ans += bit.rangesum(ca[i], t+1);
    bit.add(ca[i], 1);
  }
  cout << ans << endl;
  return 0;
}
accuracy: 0.6170212765957447
time_ms: 610
memory_kb: 52376
final_rank: 19

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n; cin >> n;
  vector<ll> a(n), b(n);
  REP(i,n) cin >> a[i];
  REP(i,n) cin >> b[i];
  ll suma = accumulate(all(a), 0LL);
  ll sumb = accumulate(all(b), 0LL);
  if(suma != sumb) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> ca(n+1), cb(n+1);
  REP(i,n) ca[i+1] = ca[i] + a[i];
  REP(i,n) cb[i+1] = cb[i] + b[i];


  map<ll, int> mp;
  int t = 1;
  for(int i=1; i<n; i++) { 
    mp[cb[i]] = t++;
  }
  BIT<ll> bit(t+1);
  ll ans = 0;
  for(int i=1; i<n; i++) {
    if(mp.find(ca[i]) == mp.end()) {
      cout << -1 << endl;
      return 0;
    }
    ca[i] = mp[ca[i]];
    ans += i-1 - bit.sum(ca[i]-1);
    bit.add(ca[i], 1);
  }
  cout << ans << endl;
  return 0;
}
accuracy: 0.6170212765957447
time_ms: 240
memory_kb: 33696
final_rank: 18

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n; cin >> n;
  vector<ll> a(n), b(n);
  REP(i,n) cin >> a[i];
  REP(i,n) cin >> b[i];
  ll suma = accumulate(all(a), 0LL);
  ll sumb = accumulate(all(b), 0LL);
  if(suma != sumb) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> ca(n+1), cb(n+1);
  REP(i,n) ca[i+1] = ca[i] + a[i];
  REP(i,n) cb[i+1] = cb[i] + b[i];

  {
    map<ll,int> cnt1, cnt2;
    for(int i=1; i<=n; i++) {
      cnt1[ca[i]]++;
      cnt2[cb[i]]++;
    }
    for(auto e: cnt1) {
      if(e.second != cnt2[e.first]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  map<ll, int> mp;
  int t = 1;
  for(int i=1; i<=n; i++) { 
    mp[cb[i]] = t++;
  }
  BIT<ll> bit(t+1);
  ll ans = 0;
  for(int i=1; i<n; i++) {
    if(mp.find(ca[i]) == mp.end()) {
      cout << -1 << endl;
      return 0;
    }
    ca[i] = mp[ca[i]];
    ans += i-1 - bit.sum(ca[i]-1);
    bit.add(ca[i], 1);
  }
  cout << ans << endl;
  return 0;
}
accuracy: 0.6170212765957447
time_ms: 610
memory_kb: 52408
final_rank: 20