結果

問題 No.3720 Balanced Reduction
コンテスト
ユーザー askr58
提出日時 2026-09-18 23:03:46
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 5,633 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,924 ms
コンパイル使用メモリ 352,164 KB
実行使用メモリ 25,776 KB
最終ジャッジ日時 2026-09-18 23:04:11
合計ジャッジ時間 7,155 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 10 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <bitset>
#include <random>
#include <chrono>
#include <iomanip>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <ranges>
#include <algorithm>
#include <vector>
using namespace std;
using ll=long long;

#include <atcoder/all>
using mint=atcoder::modint998244353;

ostream& operator<<(ostream& os,const mint& x){
	os<<x.val();
	return os;
}
istream& operator>>(istream& is,mint& x){
	int t;
	is>>t;
	x=t;
	return is;
}

template <typename S,typename T>
ostream& operator<<(ostream& os,const pair<S,T>& p);
template <typename S,typename T>
istream& operator>>(istream& is,pair<S,T>& p);
template <typename T,size_t n>
ostream& operator<<(ostream& os,const array<T,n>& arr);
template <typename T,size_t n>
istream& operator>>(istream& is,array<T,n>& arr);
template <typename T>
ostream& operator<<(ostream& os,const vector<T>& vec);
template <typename T>
istream& operator>>(istream& is,vector<T>& vec);

template <typename S,typename T>
ostream& operator<<(ostream& os,const pair<S,T>& p){
	os<<p.first<<" "<<p.second;
	return os;
}
template <typename S,typename T>
istream& operator>>(istream& is,pair<S,T>& p){
	is>>p.first>>p.second;
	return is;
}

template <typename T,size_t n>
ostream& operator<<(ostream& os,const array<T,n>& arr){
	for(int i=0;i<n;i++)os<<arr[i]<<(i+1==n?"":" ");
	return os;
}
template <typename T,size_t n>
istream& operator>>(istream& is,array<T,n>& arr){
	for(int i=0;i<n;i++)is>>arr[i];
	return is;
}
template <typename T>
ostream& operator<<(ostream& os,const vector<T>& vec){
	for(int i=0;i<(int)vec.size();i++)os<<vec[i]<<(i+1==(int)vec.size()?"":" ");
	return os;
}
template <typename T>
istream& operator>>(istream& is,vector<T>& vec){
	for(int i=0;i<(int)vec.size();i++)is>>vec[i];
	return is;
}

template<class... Vecs>
void input_vec(Vecs&... vs) {
    const auto n = get<0>(tie(vs...)).size();

    for (size_t i = 0; i < n; ++i)
        ((cin >> vs[i]), ...);
}

template <class... Vecs>
void output_vec(const Vecs&... vs) {
    const auto n = get<0>(tie(vs...)).size();

    for (size_t i = 0; i < n; ++i) {
        bool first = true;
        (((cout << (exchange(first, false) ? "" : " ") << vs[i])), ...);
        cout << endl;
    }
}

template <typename T>
vector<T> make_unique(vector<T> vec){
	ranges::sort(vec);
	vec.erase(unique(vec.begin(),vec.end()),vec.end());
	return vec;
}

vector<int> Iota(int n,int s=0){
	vector<int> res(n);
	iota(res.begin(),res.end(),s);
	return res;
}

vector<ll> Iotall(int n,ll s=0){
	vector<ll> res(n);
	iota(res.begin(),res.end(),s);
	return res;
}
void YESNO(bool f){
	if(f)cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}

using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using vvvl=vector<vector<vector<ll>>>; 
using vi=vector<int>;
using vvi=vector<vector<int>>;
using vvvi=vector<vector<vector<int>>>;
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	int n;
	ll k;
	cin>>n>>k;
	vl a(n);
	cin>>a;
	vi d(n);
	vvi graph(n);
	for(int i=0;i<n;i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		graph[u].push_back(v);
		graph[v].push_back(u);
		d[u]++;d[v]++;
	}
	auto f=[k](ll x)->ll{
		if(x==0)return 0;
		else if(x<k)return -1;
		else return (x-1)/(2*k)+1;
	};
	ll ans=0;
	queue<int> bfs;
	for(int i=0;i<n;i++)if(d[i]==1)bfs.push(i);
	while(!bfs.empty()){
		int x=bfs.front();
		bfs.pop();
		for(int y:graph[x]){
			if(d[y]==-1)continue;
			if(f(a[x])==-1){
				cout<<-1<<endl;
				return 0;
			}
			ans+=f(a[x]);
			a[y]-=a[x];
			d[y]--;
			if(d[y]==1)bfs.push(y);
		}
		d[x]=-1;
	}
	vl b;
	for(int i=0;i<n;i++){
		if(d[i]==-1)continue;
		int cur=i;
		while(1){
			d[cur]=-1;
			b.push_back(a[cur]);
			int nxt=-1;
			for(int v:graph[cur])if(d[v]==2)nxt=v;
			if(nxt==-1)break;
			cur=nxt;
		}
	}
	if(ranges::min(b)<0){
		cout<<-1<<endl;
		return 0;
	}
	n=b.size();
	if(n&1){
		ll s=0;
		for(int i=1;i<n;i++){
			if(i&1)s+=b[i];
			else s-=b[i];
		}
		for(int i=0;i<n;i++){
			ll t=b[i]+s;
			if(t&1){
				cout<<-1<<endl;
				return 0;
			}
			t=f(t/2);
			if(t==-1){
				cout<<-1<<endl;
				return 0;
			}
			ans+=t;
			if(i<n-1){
				s-=b[i+1];
				s=-s;
				s-=b[i];
			}
		}
	}else if(n==2){
		if(b[0]!=b[1]){
			cout<<-1<<endl;
			return 0;
		}
		ll t=f(b[0]);
		if(t==-1){
			cout<<-1<<endl;
			return 0;
		}
		ans+=t;
	}else{
		ll l=k,r=b[0];
		using P=pair<ll,ll>;
		vector<P> c(n);
		c[0]=P{1,0};
		for(int i=1;i<n;i++){
			c[i].first=-c[i-1].first;
			c[i].second=b[i]-c[i-1].second;
			if(c[i].first==1)l=max(l,k-c[i].second);
			else r=min(r,c[i].second-k);
		}
		if(c[0].second+c[n-1].second!=b[0]){
			cout<<-1<<endl;
			return 0;
		}
		//cout<<b<<endl;
		//for(auto p:c)cout<<p<<endl;
		vl s(2);
		for(int i=0;i<n;i++){
			if(c[i].first==1){
				s[0]=max(s[0],-c[i].second);
			}else{
				s[1]=min(s[1],c[i].second);
			}
		}
		ll res=1e18;
		for(int i=0;i<2;i++){
			ll rres=0;
			for(int j=0;j<n;j++){
				ll t=f(c[j].first*s[i]+c[j].second);
				if(f(t)==-1){
					rres=-1;
					break;
				}
				rres+=f(t);
			}
			if(rres!=-1)res=min(res,rres);
		}
		if(l<=r){
			ll sum=0;
			vector<P> evs;
			for(int i=0;i<n;i++){
				ll t=c[i].first*l+c[i].second;
				sum+=f(t);
				if(c[i].first==1){
					ll u=l+(2*k-t%(2*k))%(2*k)+1;
					if(u<=r)evs.push_back(P{u,1});
				}else{
					ll u=l+(t-1)%(2*k)+1;
					if(u<=r)evs.push_back(P{u,-1});
				}
			}
			//cout<<l<<" "<<r<<" "<<sum<<endl;
			ll rres=sum;
			ranges::sort(evs);
			for(auto[_,i]:evs){
				sum+=i;
				rres=min(rres,sum);
			}
			res=min(res,rres);
		}
		if(res==1e18){
			cout<<-1<<endl;
			return 0;
		}
		ans+=res;
	}


		
	cout<<ans<<endl;
}
0