Problem:
F: MOD Rush
問題
長さ
N
の正の整数列
A
と、長さ
M
の正の整数列
B
が与えられます。
すべての
(i, j)
(1 \leq i \leq N, 1 \leq j \leq M)
について、
A_i
を
B_j
で割ったあまりを求め、それらの和を出力してください。
入力形式
N
M
A_1
A_2
...
A_N
B_1
B_2
...
B_M
制約
1 \leq N, M \leq 2 \times 10^5
1 \leq A_i, B_i \leq 2 \times 10^5
入力はすべて整数で与えられる
出力形式
答えを
1
行に出力してください。最後に改行してください。
入力例 1
3 3
5 1 6
2 3 4
出力例 1
9
数列
A
の
1
番目の要素を、数列
B
の各要素で割ったあまりを考えます。
5
を
2
で割るとあまりは
1
、
3
で割るとあまりは
2
、
4
で割るとあまりは
1
です。
同様に
2
番目の要素についても考えると、あまりはそれぞれ
1, 1, 1
です。
3
番目の要素についても考えると、あまりはそれぞれ
0, 0, 2
です。
あまりを合計すると
1 + 2 + 1 + 1 + 1 + 1 + 0 + 0 + 2 = 9
となるので、
9
を出力します。
入力例 2
2 4
2 7
3 3 4 4
出力例 2
16
数列内には同じ値が複数含まれていることがありますが、それぞれの要素に対してあまりを計算して和を求めます。
入力例 3
3 1
12 15 21
3
出力例 3
0

Submitted solutions:

code_snippet: #line 1 "2957.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2957"

#line 2 "/home/zawatin/compro/cp-documentation/Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 2 "/home/zawatin/compro/cp-documentation/Src/Number/EnumerateQuotients.hpp"

#line 2 "/home/zawatin/compro/cp-documentation/Src/Number/IntegerDivision.hpp"

#include <type_traits>
#include <cassert>

namespace zawa {

template <class T>
constexpr T DivFloor(T a, T b) {
    static_assert(std::is_integral_v<T>, "DivFloor argument must be Integer");
    assert(b != T{});
    if constexpr (std::is_unsigned_v<T>) {
        return a / b;
    }
    else {
        if (b < 0) {
            a *= -1;
            b *= -1;
        }
        return (a >= 0 ? a / b : (a - b + 1) / b);
    }
}

template <class T>
constexpr T DivCeil(T a, T b) {
    static_assert(std::is_integral_v<T>, "DivCeil argument must be Integer");
    assert(b != T{});
    if constexpr (std::is_unsigned_v<T>) {
        return (a + b - 1) / b;
    }
    else {
        if (b < 0) {
            a *= -1;
            b *= -1;
        }
        return (a >= 0 ? (a + b - 1) / b : a / b);
    }
}

} // namespace zawa
#line 5 "/home/zawatin/compro/cp-documentation/Src/Number/EnumerateQuotients.hpp"

#line 7 "/home/zawatin/compro/cp-documentation/Src/Number/EnumerateQuotients.hpp"
#include <vector>
#include <utility>
#line 10 "/home/zawatin/compro/cp-documentation/Src/Number/EnumerateQuotients.hpp"

namespace zawa {

template <class Value>
class EnumerateQuotients {
public:
    class Data {
    private:
        Value quotient_;
        Value l_, r_;
    public:
        Data() = default;
        constexpr Data(Value quotient, Value l, Value r) : quotient_{ quotient }, l_{ l }, r_{ r } {
            assert(l < r);
        }
        
        constexpr Value quotient() const noexcept {
            return quotient_;
        }

        constexpr Value l() const noexcept {
            return l_;
        }

        constexpr Value r() const noexcept {
            return r_;
        }

        constexpr std::pair<Value, Value> range() const noexcept {
            return std::pair{ l_, r_ };
        }

        constexpr Value len() const noexcept {
            return r_ - l_;
        }
    };

private:
    std::vector<Data> seq_;
    Value n_;

    constexpr void templateTypeAssert() const noexcept {
        static_assert(std::is_integral_v<Value>, "Template argument must be unsigned integer type.");
        static_assert(std::is_unsigned_v<Value>, "Template argument must be unsigned integer type.");
    }

    constexpr void floorBuild() noexcept {
        Value l{1U};
        while (l <= n_) {
            Value quotient{ DivFloor(n_, l) };
            Value r{ DivFloor(n_, quotient) + 1 };
            seq_.emplace_back(quotient, l, r);
            l = r;
        } 
    }

    constexpr void ceilBuild() noexcept {
        Value l{1U};
        while (l < n_) {
            Value quotient{ DivCeil(n_, l) };
            Value r{ DivCeil(n_, quotient - 1) };
            seq_.emplace_back(quotient, l, r);
            l = r;
        }
        if (n_) {
            seq_.emplace_back(1U, n_, n_ + 1);
        }
    }

public:
    constexpr EnumerateQuotients() : seq_{}, n_{} {
        templateTypeAssert();
    }

    constexpr EnumerateQuotients(Value n, bool floor = true) : seq_{}, n_{ n } {
        templateTypeAssert();
        floor ? floorBuild() : ceilBuild();
        seq_.shrink_to_fit();
    }

    constexpr Value n() const noexcept {
        return n_;
    }

    constexpr Data operator[](u32 i) const noexcept {
        return seq_[i];
    }

    constexpr Value quotient(u32 i) const noexcept {
        assert(i < seq_.size()); 
        return seq_[i].quotient();
    }

    constexpr Value l(u32 i) const noexcept {
        assert(i < seq_.size()); 
        return seq_[i].l();
    }

    constexpr Value r(u32 i) const noexcept {
        assert(i < seq_.size()); 
        return seq_[i].r();
    }

    constexpr std::pair<Value, Value> range(u32 i) const noexcept {
        assert(i < seq_.size());
        return std::move(seq_[i].range());
    }

    constexpr Value len(u32 i) const noexcept {
        assert(i < seq_.size());
        return seq_[i].len();
    }

    constexpr usize size() const noexcept {
        return seq_.size();
    }

    constexpr typename decltype(seq_)::const_iterator begin() const noexcept {
        return seq_.begin();
    }

    constexpr typename decltype(seq_)::const_iterator end() const noexcept {
        return seq_.end();
    }

};

} // namespace zawa
#line 5 "2957.test.cpp"

#include <iostream>
#line 8 "2957.test.cpp"

using namespace zawa;

const usize sz{200010};

int main() {
    usize N, M; std::cin >> N >> M;
    u64 ans{};

    std::vector<i64> imos(sz);
    for (u32 i{} ; i < N ; i++) {
        u32 A; std::cin >> A;
        ans += (u64)M * A;

        EnumerateQuotients eq(A);
        for (u32 j{} ; j < eq.size() ; j++) {
            imos[eq.l(j)] += eq.quotient(j);
            imos[eq.r(j)] -= eq.quotient(j);
        }
    } 

    for (u32 i{} ; i + 1 < sz ; i++) {
        imos[i + 1] += imos[i];
    }

    for (u32 i{} ; i < M ; i++) {
        u32 B; std::cin >> B;
        ans -= imos[B] * B;
    }

    std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 1790
memory_kb: 4844
final_rank: 17

code_snippet: #line 1 "2957.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2957"

#line 2 "/home/zawatin/compro/cp-documentation/Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 2 "/home/zawatin/compro/cp-documentation/Src/Number/EnumerateQuotients.hpp"

#line 4 "/home/zawatin/compro/cp-documentation/Src/Number/EnumerateQuotients.hpp"

#include <type_traits>
#include <vector>
#include <utility>
#include <cassert>

namespace zawa {

template <class Value>
class EnumerateQuotients {
public:
    class Data {
    private:
        Value quotient_;
        Value l_, r_;
    public:
        Data() = default;
        constexpr Data(Value quotient, Value l, Value r) : quotient_{ quotient }, l_{ l }, r_{ r } {
            assert(l < r);
        }
        
        constexpr Value quotient() const noexcept {
            return quotient_;
        }

        constexpr Value l() const noexcept {
            return l_;
        }

        constexpr Value r() const noexcept {
            return r_;
        }

        constexpr std::pair<Value, Value> range() const noexcept {
            return std::pair{ l_, r_ };
        }

        constexpr Value len() const noexcept {
            return r_ - l_;
        }
    };

private:
    std::vector<Data> seq_;
    Value n_;

    constexpr void templateTypeAssert() const noexcept {
        static_assert(std::is_integral_v<Value>, "Template argument must be unsigned integer type.");
        static_assert(std::is_unsigned_v<Value>, "Template argument must be unsigned integer type.");
    }

    constexpr void floorBuild() noexcept {
        Value l{1U};
        while (l <= n_) {
            Value quotient{ n_ / l };
            Value r{ n_ / quotient + 1 };
            seq_.emplace_back(quotient, l, r);
            l = r;
        } 
    }

    constexpr void ceilBuild() noexcept {
        Value l{1U};
        while (l < n_) {
            Value quotient{ (n_ + l - 1) / l };
            Value r{ (n_ + quotient - 2) / (quotient - 1) };
            seq_.emplace_back(quotient, l, r);
            l = r;
        }
        seq_.emplace_back(1U, n_, n_ + 1);
    }

public:
    constexpr EnumerateQuotients() : seq_{}, n_{} {
        templateTypeAssert();
    }

    constexpr EnumerateQuotients(Value n, bool floor = true) : seq_{}, n_{ n } {
        templateTypeAssert();
        floor ? floorBuild() : ceilBuild();
    }

    constexpr Value n() const noexcept {
        return n_;
    }

    constexpr Data operator[](u32 i) const noexcept {
        return seq_[i];
    }

    constexpr Value quotient(u32 i) const noexcept {
        assert(i < seq_.size()); 
        return seq_[i].quotient();
    }

    constexpr Value l(u32 i) const noexcept {
        assert(i < seq_.size()); 
        return seq_[i].l();
    }

    constexpr Value r(u32 i) const noexcept {
        assert(i < seq_.size()); 
        return seq_[i].r();
    }

    constexpr std::pair<Value, Value> range(u32 i) const noexcept {
        assert(i < seq_.size());
        return std::move(seq_[i].range());
    }

    constexpr Value len(u32 i) const noexcept {
        assert(i < seq_.size());
        return seq_[i].len();
    }

    constexpr usize size() const noexcept {
        return seq_.size();
    }

    constexpr typename decltype(seq_)::const_iterator begin() const noexcept {
        return seq_.begin();
    }

    constexpr typename decltype(seq_)::const_iterator end() const noexcept {
        return seq_.end();
    }

};

} // namespace zawa
#line 5 "2957.test.cpp"

#include <iostream>
#line 8 "2957.test.cpp"

using namespace zawa;

const usize sz{200010};

int main() {
    usize N, M; std::cin >> N >> M;
    u64 ans{};

    std::vector<i64> imos(sz);
    for (u32 i{} ; i < N ; i++) {
        u32 A; std::cin >> A;
        ans += (u64)M * A;

        EnumerateQuotients eq(A);
        for (u32 j{} ; j < eq.size() ; j++) {
            imos[eq.l(j)] += eq.quotient(j);
            imos[eq.r(j)] -= eq.quotient(j);
        }
    } 

    for (u32 i{} ; i + 1 < sz ; i++) {
        imos[i + 1] += imos[i];
    }

    for (u32 i{} ; i < M ; i++) {
        u32 B; std::cin >> B;
        ans -= imos[B] * B;
    }

    std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 1610
memory_kb: 4696
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T>
vector<pair<pair<T, T>, T>> QuotientRange(T N) {
		vector<pair<pair<T, T>, T>> res;
		T l = 1;
		while (l <= N) {
				T q = N / l;
				T r = N / q + 1;
				res.emplace_back(pair{ l, r }, q);
				l = r;
		}
		return res;
}

int main(){
		int N, M; cin >> N >> M;
		vector<int> A(N), B(M);

		for (int i = 0 ; i < N ; i++) {
				cin >> A[i];
		}
		for (int i = 0 ; i < M ; i++) {
				cin >> B[i];
		}

		const int sz = (int)2e5 + 100;
		vector<ll> imos(sz);

		for (int i = 0 ; i < N ; i++) {
				auto vec = QuotientRange(A[i]);
				imos[vec[0].first.first] += vec[0].second;
				for (int i = 0 ; i + 1 < (int)vec.size() ; i++) {
					imos[vec[i].first.second] += vec[i + 1].second - vec[i].second;
				}
				imos[vec.back().first.second] -= vec.back().second;
		}


		for (int i = 0 ; i + 1 < sz ; i++) imos[i + 1] += imos[i];

		ll ans = 0LL;
		for (int i = 0 ; i < N ; i++) {
				ans += (ll)M * (ll)A[i];
		}

		for (int i = 0 ; i < M ; i++) {
				ans -= (ll)imos[B[i]] * (ll)B[i];
		}

		cout << ans << endl;
}
accuracy: 1
time_ms: 1650
memory_kb: 6148
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T>
vector<pair<pair<T, T>, T>> QuotientRange(T N) {
		vector<pair<pair<T, T>, T>> res;
		T l = 1;
		while (l <= N) {
				T q = N / l;
				T r = N / q + 1;
				res.emplace_back(pair{ l, r }, q);
				l = r;
		}
		return res;
}

int main(){
		int N, M; cin >> N >> M;
		vector<int> A(N), B(M);

		for (int i = 0 ; i < N ; i++) {
				cin >> A[i];
		}
		for (int i = 0 ; i < M ; i++) {
				cin >> B[i];
		}

		const int sz = (int)2e5 + 100;
		vector<int> imos(sz);

		for (int i = 0 ; i < N ; i++) {
				auto vec = QuotientRange(A[i]);
				imos[vec[0].first.first] += vec[0].second;
				for (int i = 0 ; i + 1 < (int)vec.size() ; i++) {
					imos[vec[i].first.second] += vec[i + 1].second - vec[i].second;
				}
				imos[vec.back().first.second] -= vec.back().second;
		}


		for (int i = 0 ; i + 1 < sz ; i++) imos[i + 1] += imos[i];

		ll ans = 0LL;
		for (int i = 0 ; i < N ; i++) {
				ans += (ll)M * (ll)A[i];
		}

		for (int i = 0 ; i < M ; i++) {
				ans -= (ll)imos[B[i]] * (ll)B[i];
		}

		cout << ans << endl;
}
accuracy: 0.3333333333333333
time_ms: 1070
memory_kb: 5084
final_rank: 20

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

using namespace std;

using ll = long long;
const int N = 3e5;

vector<ll> input_counts(int n){
  vector<ll> res(N,0);
  for(int i=0;i<n;++i){
    int v;
    cin >> v;
    ++res[v];
  }
  return res;
}

ll func(){
  int n;
  int m;
  cin >> n >> m;
  vector<ll> as = input_counts(n);
  vector<ll> bs = input_counts(m);
  vector<ll> subs(N,0);
  for(int i=1;i<N;++i){
    for(int j=i;j<N;j+=i){
      subs[j] += i * bs[i];
    }
  }
  ll res = 0;
  ll sum = 0;
  for(int i=1;i<N;++i){
    sum += m;
    sum -= subs[i];
    res += sum * as[i];
  }
  return res;
}

int main(){
  cout << func() << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 10176
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=10000000;

#define rep(i,a) for (int i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}

namespace po167{
template <class T,T (*op)(T,T),T(*e)()>
struct segment_tree{
	int _n,size;
	std::vector<T> seg;
	int ceil_pow2(int a){
		int b=1;
		while(a>b){
			b<<=1;
		}
		return b;
	}
	void update(int k){seg[k]=op(seg[k*2],seg[k*2+1]);};
	segment_tree(int n) :_n(n){
		size=ceil_pow2(n);
		seg=std::vector<T>(size*2,e());
	}
	segment_tree(std::vector<T> &p) :_n((int) p.size()){
		size=ceil_pow2(_n);
		seg=std::vector<T>(size*2,e());
		for(int i=0;i<_n;i++) seg[i+size]=p[i];
		for(int i=size-1;i>0;i--) update(i);
	}
	void set(int ind,T val){
		assert(0<=ind&&ind<_n);
		ind+=size;
		seg[ind]=val;
		while(ind!=1){
			ind>>=1;
			update(ind);
		}
	}
    void addl(int ind,T val){
        set(ind,op(get(ind),val));
    }
    void addr(int ind,T val){
        set(ind,op(val,get(ind)));
    }
	T get(int ind){
		assert(0<=ind&&ind<_n);
		return seg[ind+size];
	}
	T query(int l,int r){
		assert(0<=l&&l<=r&&r<=_n);
		T l_val=e();
		T r_val=e();
		l+=size,r+=size;
		while(l<r){
			if(l&1) l_val=op(l_val,seg[l]),l+=1;
			if(r&1) r-=1,r_val=op(seg[r],r_val);
			r>>=1;
			l>>=1;
		}
		return op(l_val,r_val);
	}
	template <bool (*f)(T)> int max_right(int l) {
        return max_right(l, [](T x) { return f(x); });
    }
	template <class F> int max_right(int l, F f) {
		assert(0<=l&&l<=_n);
		assert(f(e()));
		if(f(query(l,_n))) return _n;
		T val=e();
		l+=size;
		while(true){
			while(l%2==0) l>>=1;
			if(!f(op(val,seg[l]))){
				while(l<size){
					l*=2;
					if(f(op(val,seg[l]))){
						val=op(val,seg[l]);
						l++;
					}
				}
				return l-size;
			}
			val=op(val,seg[l]);
			l++;
		}
	}
	template <bool (*f)(T)> int min_left(int r) {
        return min_left(r, [](T x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        T sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(seg[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(seg[r], sm))) {
                        sm = op(seg[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(seg[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

};
}
using po167::segment_tree;


using F= ll;
F op(F a,F b){return a+b;}
F e(){return 0;}


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	ll N,M;
	cin>>N>>M;
	int L=2e5+10;
	segment_tree<F,op,e> seg(L);
	ll S=0;
	rep(i,N){
		ll a;
		cin>>a;
		seg.addl(a,1);
		S+=a;
	}
	vector<ll> base(L);
	rep(i,L){
		if(i==0) continue;
		for(int j=1;j*i<L;j++){
			int r=min(L,j*i+i);
			base[i]+=seg.query(j*i,r)*(ll)(j*i);
		}
	}
	//rep(k,10) cout<<k<<" "<<base[k]<<"\n";
	ll ans=S*M;
	rep(i,M){
		int b;
		cin>>b;
		ans-=base[b];
	}
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 80
memory_kb: 8816
final_rank: 3

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#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 sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-3, PI = acos(-1);
//ここから編集

ll arr[501010];
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(6);
    
    ll n, m; cin >> n >> m;
    vector<ll> a(n), b(m);
    ll sum = 0;
    REP(i,n) {
        cin >> a[i];
        arr[a[i]+1]+=1;
        sum += a[i];
    }
    sum *= m;
    
    REP(i,500010) arr[i+1] += arr[i];
    map<ll,ll> mp;
    REP(i,m) cin >> b[i];
    REP(i,m) mp[b[i]]++;
    for(auto e: mp) {
        
        for(ll j=e.first; j<=500005; j+=e.first){
            
            ll cnt = arr[j] - arr[j-e.first];
            
            sum -= e.second*cnt*(j-e.first);
        }

    }
    cout << sum << endl;

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 22096
final_rank: 13

code_snippet: #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; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }


const int MAX = 510000;
int main() {
    int N, M;
    cin >> N >> M;
    long long S = 0;
    vector<long long> Anum(MAX, 0);
    vector<long long> Bnum(MAX, 0);
    for (int i = 0; i < N; ++i) { int a; cin >> a; Anum[a]++; S += a; }
    for (int i = 0; i < M; ++i) { int a; cin >> a; Bnum[a]++; }
    vector<long long> sum(MAX + 1, 0);
    for (int i = 0; i < MAX; ++i) sum[i+1] = sum[i] + Anum[i];

    long long res = 0;
    for (long long b = 1; b < MAX; ++b) {
        if (Bnum[b] == 0) continue;
        long long qsum = 0;
        for (long long q = 0; b * (q + 1) <= MAX; ++q) {
            qsum += q * (sum[b * (q + 1)] - sum[b * q]);
        }
        res += Bnum[b] * (S - b * qsum);
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 15080
final_rank: 8

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Starry Sky Tree */
//0-index

struct StarrySkyTree{
  typedef ll Type;
  int segn2;
  vector<pair<ll,int>> data;
  vector<Type> s_data;
  function<Type(Type, Type)> merge;

  StarrySkyTree(function<Type(Type, Type)> merge, int n): merge(merge)
  {
    for(segn2=1; segn2<n; segn2*=2);
    data.assign(segn2*2, {0, 0});
    s_data.assign(segn2*2, 0);
  }

  StarrySkyTree(int n):
    StarrySkyTree([](Type a, Type b){ return a + b; }, n) {}

  //get value of [a,b)
  pair<ll,int> query(int a, int b, int l = 0, int r = -1, int k = 0){
    if(r == -1) r = segn2;
    if(r <= a || b <= l) return {0, 0}; //大きさに注意
    if(a <= l && r <= b) return {data[k].first + data[k].second * s_data[k], data[k].second};

    auto p1 = query(a, b, l, (l+r)/2, k*2+1);
    auto p2 = query(a, b, (l+r)/2, r, k*2+2);

    int num = p1.second + p2.second;
    ll sum = p1.first + p2.first + num * s_data[k];

    return {sum, num};
  }

  //add x to [a,b)
  pair<ll,int> add(int a, int b, Type x, int l = 0, int r = -1, int k = 0){
    if(r == -1) r = segn2;
    if(a <= l && r <= b) {
      s_data[k] += x;
    } else if(a < r && l < b) {
      auto p1 = add(a, b, x, l, (l+r)/2, k*2+1);
      auto p2 = add(a, b, x, (l+r)/2, r, k*2+2);
      int num = p1.second + p2.second;
      ll sum = p1.first + p2.first;
      data[k] = {sum, num};
    }

    return {data[k].first + s_data[k] * data[k].second, data[k].second};
  }

  //add x to [a,b)
  pair<ll,int> add2(int a, int b, Type x, int l = 0, int r = -1, int k = 0){
    if(r == -1) r = segn2;
    if(a <= l && r <= b)
      data[k].second += x;
    else if(a < r && l < b) {
      auto p1 = add2(a, b, x, l, (l+r)/2, k*2+1);
      auto p2 = add2(a, b, x, (l+r)/2, r, k*2+2);
      int num = p1.second + p2.second;
      ll sum = p1.first + p2.first;
      data[k] = {sum, num};
    }

    return {data[k].first + s_data[k] * data[k].second, data[k].second};
  }

};

ll test(int N) {
  ll sum = 0;

  for (int i=1; i<=N; i++) {
    sum += N / i * 2;
  }

  return sum;
}

int counter[SIZE];
int B[SIZE];

int main(){
  int N, M;
  int L = 0;

  scanf("%d%d", &N, &M);

  for (int i=0; i<N; i++) {
    int a;
    scanf("%d", &a);
    counter[a]++;
    L = max(L, a+1);
  }

  B[0] = 1;
  M++;
  for (int i=1; i<M; i++) {
    scanf("%d", B+i);
  }

  sort(B, B+M);

  StarrySkyTree seg(L);

  ll ans = 0;

  for (int i=0; i<L; i++) {
    seg.add2(i, i+1, counter[i]);
  }

  for (int i=1; i<M; i++) {
    debug(i);
    if (B[i-1] != B[i]) {

      int a = B[i-1];
      int b = B[i];

      set<int> ss;

      for (ll j=0; j*a<L; j++)
        ss.insert(j * a);

      for (ll j=0; j*b<L; j++)
        ss.insert(j * b);

      ss.insert(L);


      int prev = -1;
      for (int t : ss) {
        if (prev >= 0) {
          int d = prev % b - prev % a;
          seg.add(prev, t, d);
        }
        prev = t;
      }
    }

    debug(B[i]);
    debug(seg.query(0, L));
    ans += seg.query(0, L).first;
  }

  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 1250
memory_kb: 26100
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  Int n,m;
  cin>>n>>m;
  vector<Int> as(n),bs(m);
  for(Int i=0;i<n;i++) cin>>as[i];
  for(Int i=0;i<m;i++) cin>>bs[i];

  sort(as.begin(),as.end());
  vector<Int> ss(n+1,0);
  for(Int i=0;i<n;i++) ss[i+1]=ss[i]+as[i];

  map<Int, Int> cnt;
  for(Int b:bs) cnt[b]++;

  const Int MAX = 3e5;
  Int ans=0;
  for(auto p:cnt){
    Int b=p.first;
    Int res=0;
    for(Int k=0;b*k<=MAX;k++){
      // [lb, ub)
      Int lb=b*k;
      Int ub=b*(k+1);
      auto latte=lower_bound(as.begin(),as.end(),lb)-as.begin();
      auto malta=lower_bound(as.begin(),as.end(),ub)-as.begin();
      res+=ss[malta]-ss[latte];
      res-=lb*(malta-latte);
    }
    ans+=res*p.second;
  }

  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 390
memory_kb: 19668
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>

int main() {
	int n, m; std::cin >> n >> m;
	std::vector<int> a(n), b(m); for (auto& ai : a) std::cin >> ai; for (auto& bi : b) std::cin >> bi;
	std::vector<long long int> count_a(200001, 0), count_b(200001, 0);
	long long int result = 0LL;
	long long int sum = std::accumulate(a.begin(), a.end(), 0LL);
	for (const auto ai : a) count_a[ai]++;
	for (const auto bi : b) count_b[bi]++;
	for (auto i = 1; i < count_a.size(); ++i) count_a[count_a.size() - i - 1] += count_a[count_a.size() - i];
	for (auto i = 0; i < count_b.size(); ++i) if (count_b[i] != 0) {
		if (i * i <= count_a.size()) {
			for (const auto ai: a) result += ai % i * count_b[i];
		}
		else {
			result += sum * count_b[i];
			for (auto j = 1; i * j < count_a.size(); ++j) result -= count_a[i * j] * i * count_b[i];
		}
	}
	std::cout << result << std::endl;
}
accuracy: 1
time_ms: 280
memory_kb: 7592
final_rank: 4

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define MAX 200005

int N,M;
ll table[MAX],count_table[MAX];

int main(){

	for(int i = 0; i < MAX; i++){

		table[i] = 0;
		count_table[i] = 0;
	}

	scanf("%d %d",&N,&M);

	ll sum = 0,tmp,minimum = BIG_NUM,maximum = -BIG_NUM;

	for(int i = 0; i < N; i++){

		scanf("%lld",&tmp);

		minimum = min(minimum,tmp);
		maximum = max(maximum,tmp);

		table[tmp]++;
		sum += tmp;
	}

	sum *= M;

	for(int i = 1; i < MAX; i++){

		table[i] += table[i-1];
	}

	ll left = BIG_NUM,right = -BIG_NUM;

	for(int i = 0; i < M; i++){

		scanf("%lld",&tmp);

		left = min(left,tmp);
		right = max(right,tmp);

		count_table[tmp]++;
	}

	for(ll i = left; i <= right; i++){

		if(count_table[i] == 0)continue;

		for(ll k = 1; i*k <= maximum; k++){

			ll L,R;

			L = i*k-1;

			if(i*(k+1)-1 >= MAX){

				R = MAX-1;
			}else {
				R = i*(k+1)-1;
			}

			sum -= (table[R]-table[L])*i*k*count_table[i];
		}
	}

	printf("%lld\n",sum);

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6340
final_rank: 1

code_snippet: // #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return max(low, min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { t = clamp(t, low, high); }

namespace init__ {

struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
    return V<decltype(rec)>(s, rec);
#undef rec
}

int main() {
    ll N, M;
    cin >> N >> M;
    V<ll> A(N), B(M);
    for(auto &&e : A) cin >> e;
    for(auto &&e : B) cin >> e;
    V<ll> cnt_sum_b(2e5 + 10), sum_b(2e5 + 10);
    for(auto &&e : B) cnt_sum_b[e]++;
    for(ll i = 1; i < sum_b.size(); i++) {
        sum_b[i] = i * cnt_sum_b[i] + sum_b[i - 1];
        cnt_sum_b[i] += cnt_sum_b[i - 1];
    }

    ll ans = 0;
    ll div_upper = sqrt(2e5 + 10);
    for(auto &&ele : A) {
        ans += ele * (cnt_sum_b.back() - cnt_sum_b[ele]);
        ll lower_idx = 0;
        for(ll d_val = 1; d_val < div_upper; d_val++) {
            ll lower = ele / (d_val + 1);
            ll upper = ele / d_val;
            lower_idx = lower;
            ans += ele * (cnt_sum_b[upper] - cnt_sum_b[lower]);
            ans -= d_val * (sum_b[upper] - sum_b[lower]);
        }
        for(ll i = 1; i <= lower_idx; i++) ans += (cnt_sum_b[i] - cnt_sum_b[i - 1]) * (ele % i);
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 9052
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using treap = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define fi first
#define se second
using ii = pair<int, int>;
vector<ii> dirs = {
  {1, 0}, {0, 1}, {-1, 0}, {0, -1},  // 4方向
  {1, 1}, {-1, 1}, {-1, -1}, {1, -1},  // 斜め
  {0, 0},  // 自身
};
template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template <class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); }

// debug
template <class T> ostream& operator<<(ostream& s, vector<T>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; }
template <class T> ostream& operator<<(ostream& s, vector<vector<T>>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; }
template <class T, class S> ostream& operator<<(ostream& s, pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T, class S> ostream& operator<<(ostream& s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
template <class T, class S> ostream& operator<<(ostream& s, unordered_map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
#ifdef _MY_DEBUG
  #define dump(...) cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), cerr << "*/\n\n";
#else
  #define dump(...)
  #define endl "\n"
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }

struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr int MOD = 1000000007;
// *************** TEMPLATE END *************** 

const int K = 2e5 + 10;
signed main() {
  int n, m; cin >> n >> m;
  vector<int> A(n); REP (i, n) cin >> A[i];
  unordered_map<int, int> B;
  REP (i, m) {
    int x; cin >> x;
    B[x]++;
  }
  vector<int> acc(K, m);
  acc[0] = 0;
  
  for (auto p : B) {
    for (int j = p.fi; j < K; j += p.fi) {
      acc[j] -= p.fi * p.se;
    }
  }

  REP (i, K - 1) acc[i + 1] += acc[i];
  int ans = 0;
  for (int a : A) ans += acc[a];
  cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 14020
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll,ll> P;
#define Mod 1000000007
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
#define INF 1ll<<59

int main(){
  ll i,j;
  ll n,m;
  cin>>n>>m;
  ll a[n];
  map<ll,ll> ma;
  ll ans=0;
  for(i=0;i<n;i++) {
    cin>>a[i];
    ans+=a[i]*m;
  }
  for(i=0;i<m;i++){
    ll x;
    cin>>x;
    ma[x]++;
  } 

  VI c(202020,0);
  for(auto it=ma.begin();it!=ma.end();++it){
    for(j=1;j*(it->first)<202020;j++){
      c[j*(it->first)]+=it->first*(it->second);
    }
  }
  for(i=1;i<202020;i++){
    c[i]+=c[i-1];
  }

  for(i=0;i<n;i++) ans-=c[a[i]];
  cout<<ans<<endl;  
  
  return 0;
}

//いもすほうはつよい
accuracy: 1
time_ms: 130
memory_kb: 18132
final_rank: 11

code_snippet: /* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` ``  ` ` ` ` ` ` ` ` `  dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` `` ````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` `` ```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` `  ````````.`````` `` ``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...`````  .,.`  ````....````` ``````` (Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<`` `````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ```````````````` ._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- `````````````````````````````````` ._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.`` OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____..  ````````  ..__~~_((<<!.`.``` .``.`` j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```  jOuWHHqHIdH
// gmmqkW!`(_ J>`    `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__```    zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. `   _ ?&._.I`_`````` .XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}  .`..````jqg@@@@@@
// XHWZ{..<Jk~!.`.. (<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;< ._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~` _-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
// kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
// @@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~``` ~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
// @gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
// mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
// qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~`` (jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ```` d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( `` WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
// XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<`` (v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.`` :.~+=?~.(;_(...jdQQQQQkkk
// H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
// @@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
// @H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
// @@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
// @@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
// MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
// HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
// HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
// rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%`` (Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ ` _``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>` ~````.OXtt>~._<?MM
*/

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second

template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class T>
istream& operator>>(istream& is, vector<T>& xs){
  for(auto& x: xs)
	is >> x;
  return is;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T>& xs){
  for(unsigned int i=0;i<xs.size();++i)
	os << (i?" ":"") << xs[i];
  return os;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}
void debug(istringstream&){}
template <char sep=',', class Head, class... Tail>
void debug(istringstream& iss, Head&& head, Tail&&... tail)
{
  string name;
  getline(iss, name, ',');
  cout << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}

#ifdef PYONPOI
#define DEBUG(...) \
  do{ \
    istringstream ss(#__VA_ARGS__); \
    debug<' '>(ss, __VA_ARGS__); \
    cout<<endl; \
  }while(0)
#else
#define DEBUG
#endif


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  VI as(N), bs(M);
  cin >> as >> bs;

  SORT(bs);
  const int MAX = 2e5 + 10;
  VL acc(MAX);
  for(auto a : as) acc[a+1]++;
  FOR(i,1,MAX) acc[i] += acc[i-1];

  LL ans = 0;
  map<LL,LL> mp;
  REP(j,M){
    LL b = bs[j];
    if(mp.count(b)){
      ans += mp[b];
      continue;
    }
    LL sum = 0;
    int ub = (MAX + b - 1) / b;
    for(int q=1;q<=ub;++q){
      int beg = min((LL)MAX-1, q * b);
      int ed = min((LL)MAX-1, q * b + b);
      sum += (acc[ed] - acc[beg]) * q;
    }
    ans += sum * b;
    mp[b] = sum * b;
  }

  LL sum = accumulate(ALL(as), 0ll) * M;
  cout << sum - ans << endl;

  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 18368
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll=long long;
const int SIZE=2*1e5+3;
int main(){
    int n,m; cin>>n>>m;
    vector<int> cntA(SIZE);
    map<int,int> mpB;
    for(int i=0;i<n;i++){
        int a; cin>>a;
        cntA[a]++;
    }
    for(int i=0;i<m;i++){
        int b; cin>>b;
        mpB[b]++;
    }
    vector<ll> event(SIZE);
    for(auto& e:mpB){
        int x=e.first;
        for(int i=x;i<SIZE;i+=x){
            event[i]-=ll(e.first)*e.second;
        }
    }

    ll sum=0;
    ll res=0;
    for(int i=0;i<SIZE;i++){
        sum+=event[i];
        res+=sum*cntA[i];
        sum+=m;
    }
    cout<<res<<endl;
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 14324
final_rank: 7

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m; cin >> n >> m;
  vector<int> a(n);
  long long sum = 0;
  REP(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  sum *= m;
  map<int, int> mp;
  while (m--) {
    int b; cin >> b;
    ++mp[b];
  }
  sort(ALL(a));
  for (auto pr : mp) {
    int b = pr.first;
    for (long long p = 0; p <= 200000; p += b) {
      int idx1 = lower_bound(ALL(a), p) - a.begin(), idx2 = lower_bound(ALL(a), p + b) - a.begin();
      sum -= p * (idx2 - idx1) * pr.second;
    }
  }
  cout << sum << '\n';
  return 0;
}
accuracy: 1
time_ms: 320
memory_kb: 12680
final_rank: 9

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> cnt(404040);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
    sum += a;
  }
  for (int i = 1; i < (int) cnt.size(); i++) {
    cnt[i] += cnt[i - 1];
  }
  vector<int> b(m);
  vector<long long> memo(202020, -1);
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  long long ans = sum * m;
  for (int i = 0; i < m; i++) {
    if (memo[b[i]] != -1) {
      ans -= memo[b[i]];
      continue;
    }
    memo[b[i]] = 0;
    for (int x = 1; (x + 1) * b[i] - 1 < 404040; x++) {
      memo[b[i]] += 1LL * b[i] * x * (cnt[(x + 1) * b[i] - 1] - cnt[x * b[i] - 1]);
    }
    ans -= memo[b[i]];
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 6796
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> cnt(404040);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    cnt[a]++;
    sum += a;
  }
  for (int i = 1; i < (int) cnt.size(); i++) {
    cnt[i] += cnt[i - 1];
  }
  vector<int> b(m);
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  vector<int> res(202020, -1);
  for (int i = 0; i < m; i++) {
    if (res[b[i]] != -1) continue;
    long long tot = 0;
    for (int x = 1; (x + 1) * b[i] - 1 < 404040; x++) {
      tot += 1LL * b[i] * x * (cnt[(x + 1) * b[i] - 1] - cnt[x * b[i] - 1]);
    }
    res[b[i]] = sum - tot;
  }
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    ans += res[b[i]];
  }
  cout << ans << endl;
  return 0;
}
accuracy: 0.3333333333333333
time_ms: 20
memory_kb: 5960
final_rank: 19