Problem:
Problem G
Two Sets of Cards
As an entertainment after the programming contest, you hold a game event for the participants.
Two sets of cards, red ones and blue ones, are used in the game.
Both of the two sets have the same number of cards as the number of the participants, and each card has one integer written on it.
It is known that the contents of these two sets are the same; the integers on the red cards are the same as those on the blue cards as multisets.
Even though, you don't know what integers are actually written on the cards.
The same integer may be written on two or more cards of the same color, and the integers may be negative.
You distributed one red card and one blue card to each of the participants.
As the game result depends on the sum of the integers on the two cards distributed to each participant, you asked all the participants to declare their sums.
Your task is to find a possible card distribution consistent with the participants' declarations, that is, a combination of integers on the cards distributed to each participant.
Note that such a combination might not exist, as the participants may commit miscalculations.
In such a case, report that there is no consistent combination.
Input
The input consists of multiple datasets, each in the following format.
The number of datasets does not exceed 100.
n
s
1
s
2
⋯
s
n
Here,
n
is the number of the participants, an integer between 1 and 70, inclusive.
Each of
s
i
(
i
= 1,  ,
n
) is the declared sum of the integers on the two cards distributed to the
i
-th participant, an integer between −150 and 150, inclusive.
The end of the input is indicated by a line consisting of a zero.
Output
For each dataset, if there exists no combination of integers on the cards distributed to each participant that is consistent with the participants' declarations, output "No" in one line.
Otherwise, output "Yes" in one line followed by one possible consistent combination in the following format.
a
1
a
2
⋯
a
n
b
1
b
2
⋯
b
n
Here, for
i
= 1,  ,
n,
a
i
and
b
i
represent the integers on the red and blue cards, respectively, distributed to the
i
-th participant.
Each of
a
i
and
b
i
must be an integer between −10
9
and 10
9
, inclusive.
It can be proved that, when one or more consistent combinations exist, there also exists a consistent combination satisfying this condition.
If there are two or more such combinations, you may output any one of them.
Sample Input
3
7 -2 3
3
4 8 8
4
1 2 4 8
6
-6 -2 3 2 9 -4
1
-100
0
Output for the Sample Input
Yes
1 -3 6
6 1 -3
Yes
2 4 4
2 4 4
No
Yes
3 -1 4 -1 5 -9
-9 -1 -1 3 4 5
Yes
-50
-50

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
//	n=10;
	while(n)
	{
//		vector<int>a0(n+1),b0;
//		for(int i=1;i<=n;i++)a0[i]=rand()%151-75;
//		b0=a0;
//		for(int i=n;i>1;i--)
//			swap(b0[i],b0[rand()%n+1]);
		vector<int>s(n+1);
		int sum=0,ff=0,cnt1=0;
		for(int i=1;i<=n;i++)
		{
//			s[i]=a0[i]+b0[i];
//			printf("%d ",s[i]);
			scanf("%d",&s[i]);
			s[i]+=150;
			sum+=s[i];
			if(s[i]%2==0)ff=1;
			if(s[i]%2==1)cnt1++;
		}
		vector<int>s0=s;
		if(ff)
		{
			if(sum%2==0)
			{
				puts("Yes");
				if(!cnt1)
				{
					for(int i=1;i<=n;i++)printf("%d%c",s[i]/2-75," \n"[i==n]);
					for(int i=1;i<=n;i++)printf("%d%c",s[i]/2-75," \n"[i==n]);
//					for(int i=1;i<=n;i++)assert(s[i]/2-75+s[i]/2-75==a0[i]+b0[i]);
				}
				else
				{
					vector<int>f;
					for(int i=1;i<=n;i++)if(s[i]%2!=0)f.push_back(i);
					for(int i=1;i<=n;i++)if(s[i]%2==0)
					{
						int sum1=0;
						vector<int>g;
						for(int j=f.size()/2;j<f.size();j++)
							g.push_back(f[j]);
						f.resize(f.size()/2);
						for(int j=0;j<f.size();j+=2)
						{
							sum1-=s[f[j]];
							if(j+1<g.size())sum1+=s[f[j+1]];
						}
						for(int j=0;j<g.size();j+=2)
						{
							sum1-=s[g[j]];
							if(j+1<g.size())sum1+=s[g[j+1]];
						}
//						printf("%d\n",sum1);
						if(f.size()%2)sum1=-s[i]-sum1;
						else sum1=s[i]-sum1;
//						printf("%d\n",sum1);
						f.push_back(i);
						g.push_back(i);
						vector<int>ansa(n+1),ansb(n+1);
						ansa[f[0]]=ansb[g[0]]=sum1/2;
						s[f[0]]-=ansa[f[0]],s[g[0]]-=ansb[g[0]];
						for(int i=1;i<f.size();i++)
						{
//							for(int i=1;i<=n;i++)printf("%d ",s[i]);puts("");
							ansa[f[i]]=s[f[i-1]],ansb[f[i-1]]=s[f[i-1]];
							s[f[i-1]]-=ansb[f[i-1]],s[f[i]]-=ansa[f[i]];
						}
//						for(int i=1;i<=n;i++)printf("%d ",s[i]);puts("");
						for(int i=1;i<g.size();i++)
						{
							ansb[g[i]]=s[g[i-1]],ansa[g[i-1]]=s[g[i-1]];
							s[g[i-1]]-=ansa[g[i-1]],s[g[i]]-=ansb[g[i]];
						}
//						for(int i=1;i<=n;i++)printf("%d ",s[i]);puts("");
						for(int j=i+1;j<=n;j++)if(s0[j]%2==0)ansa[j]=ansb[j]=s[j]/2;
						for(int i=1;i<=n;i++)printf("%d%c",ansa[i]-75," \n"[i==n]);
						for(int i=1;i<=n;i++)printf("%d%c",ansb[i]-75," \n"[i==n]);
//						for(int i=1;i<=n;i++)assert(ansa[i]-75+ansb[i]-75==a0[i]+b0[i]);
							
						break;
					}
				}
			}
			else puts("No");//,assert(0);
		}
		else
		{
			vector<vector<bitset<21001>>>dp(n+1,vector<bitset<21001>>(n*2+1));
			dp[0][0][0]=1;
			for(int i=0;i<n;i++)if(s[i+1]%2)
			{
				dp[i+1][0]=dp[i][0];
				dp[i+1][1]=dp[i][1];
				for(int j=2;j<n*2+1;j++)
					dp[i+1][j]=(dp[i][j]|(dp[i][j-2]<<s[i+1]));
			}
			else 
			{
				dp[i+1][0]=dp[i][0];
				dp[i+1][1]=(dp[i][1]|(dp[i][0]<<s[i+1]/2));
				for(int j=2;j<n*2+1;j++)
					dp[i+1][j]=(dp[i][j]|(dp[i][j-2]<<s[i+1])|(dp[i][j-1]<<s[i+1]/2));
			}
			if(sum%2||!dp[n][n][sum/2])puts("No");//,assert(0);
			else
			{
				vector<int>b,c,vis(n+1);
				int j=sum/2,k=n;
				for(int i=n;i>0;i--)
					if(j>=s[i]&&k>=2&&dp[i-1][k-2][j-s[i]])
						b.push_back(i),j-=s[i],k-=2;
					else if(dp[i-1][k][j])c.push_back(i);
					else vis[i]=1,j-=s[i]/2,k--;
				assert(b.size()==c.size());
				vector<int>ansa(n+1),ansb(n+1);
				for(int i=1;i<=n;i++)if(vis[i])ansa[i]=s[i]/2,ansb[i]=s[i]/2;
	//			for(int i=1;i<=n;i++)printf("%d ",ansa[i]);puts("");
	//			for(int i=1;i<=n;i++)printf("%d ",ansb[i]);puts("");
	//			for(int x:b)printf("%d ",x);puts("");
	//			for(int x:c)printf("%d ",x);puts("");
				for(int i=0;i<b.size();i++)
				{
					ansa[b[i]]=ansb[c[i]]=s[b[i]];
					s[b[i]]-=ansa[b[i]],s[c[i]]-=ansb[c[i]];
					ansa[c[i]]=ansb[b[(i+1)%b.size()]]=s[c[i]];
					s[c[i]]-=ansa[c[i]],s[b[(i+1)%b.size()]]-=ansb[b[(i+1)%b.size()]];
				}
				puts("Yes");
				for(int i=1;i<=n;i++)printf("%d%c",ansa[i]-75," \n"[i==n]);
				for(int i=1;i<=n;i++)printf("%d%c",ansb[i]-75," \n"[i==n]);
//				for(int i=1;i<=n;i++)assert(ansa[i]-75+ansb[i]-75==a0[i]+b0[i]);
			}
		}
		cin>>n;
	}
	return 0;
}
/*
 3
 7 -2 3
 3
 4 8 8
 4
 1 2 4 8
 6
 -6 -2 3 2 9 -4
 1
 -100
 0
*/
accuracy: 1
time_ms: 630
memory_kb: 29388
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define TT template<typename T>
TT using vec = vector<T>;
template<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }
template<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main() {
    while(1){
        int N;
        cin>>N;
        if(N==0)return 0;
        vector<int>S(N);
        rep(i,0,N)cin>>S[i];
        int sum=0;
        rep(i,0,N)sum+=S[i];
        if(sum%2!=0){
            cout<<"No\n";
            continue;
        }
        sum/=2;
        if(N%2==1){
            vector<int>A;
            rep(i,0,N){
                int cnt=sum;
                rep(j,0,N/2){
                    cnt-=S[(i+2*(j+1))%N];
                }
                A.push_back(cnt);
            }
            cout<<"Yes"<<endl;
            rep(i,0,N)cout<<A[i]<<" ";
            cout<<endl;
            rep(i,0,N)cout<<A[(i-1+N)%N]<<" ";
            cout<<endl;
        }else{
            bool flag=false;
            rep(x,0,N){
                if(S[x]%2==0){
                    vector<int>C;
                    vector<int>P,Q;
                    rep(i,0,N){
                        if(i==x)continue;
                        P.push_back(S[i]);
                        Q.push_back(i);
                    }
                    rep(i,0,N-1){
                        int cnt=sum-S[x]/2;
                        rep(j,0,(N-1)/2){
                            cnt-=S[Q[(i+2*(j+1))%(N-1)]];
                        }
                        C.push_back(cnt);
                    }
                    vector<int>D;
                    rep(i,0,N-1)D.push_back(C[(i-1+N-1)%(N-1)]);
                    vector<int>A(N),B(N);
                    A[x]=S[x]/2,B[x]=S[x]/2;
                    int now=0;
                    rep(i,0,N){
                        if(i==x)continue;
                        A[i]=C[now],B[i]=D[now];
                        now++;
                    }
                    cout<<"Yes"<<endl;
                    rep(i,0,N)cout<<A[i]<<" ";
                    cout<<endl;
                    rep(i,0,N)cout<<B[i]<<" ";
                    cout<<endl;
                    flag=true;
                    break;
                }
            }
            if(!flag){
                vector dp(N+1,vector<unordered_map<int,bool>>(N+1));
                dp[0][0][0]=true;
                rep(i,0,N){
                    rep(k,0,i+1){
                        for(auto[j,f]:dp[i][k]){
                            dp[i+1][k+1][j+S[i]]=true;
                            dp[i+1][k][j]=true;
                        }
                    }
                }
                if(!dp[N][N/2][sum]){
                    cout<<"No"<<endl;
                }else{
                    vector<int>V,W;
                    int p=N/2,q=sum;
                    rrep(i,0,N){
                        if(dp[i][p][q]){
                            W.push_back(i);
                            continue;
                        }
                        if(p>0&&dp[i][p-1][q-S[i]]){
                            p--;
                            q-=S[i];
                            V.push_back(i);
                            continue;
                        }
                    }
                    reverse(all(V));
                    reverse(all(W));
                    vector<int>A(N),B(N);
                    A[V[0]]=0;
                    rep(i,0,N){
                        if(i%2==0){
                            B[V[i/2]]=S[V[i/2]]-A[V[i/2]];
                            A[W[i/2]]=B[V[i/2]];
                        }else{
                            B[W[i/2]]=S[W[i/2]]-A[W[i/2]];
                            if(i!=N-1)A[V[(i+1)/2]]=B[W[i/2]];
                        }
                    }
                    cout<<"Yes"<<endl;
                    rep(i,0,N)cout<<A[i]<<" ";
                    cout<<endl;
                    rep(i,0,N)cout<<B[i]<<" ";
                    cout<<endl;
                }
            }
        }
    }
}
accuracy: 1
time_ms: 2810
memory_kb: 130816
final_rank: 5

code_snippet: //#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for (ll i=0;i<(ll)n;i++)
#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)
#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)
#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<" ";}cout<<endl;
#define vpdbg(a) rep(ii,a.size()){cout<<"{"<<a[ii].first<<","<<a[ii].second<<"} ";}cout<<endl;
#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<" ";}cout<<endl;}
#define setdbg(a) for(const auto & ii:a){cout<<ii<<" ";}cout<<endl;
#define inf 4000000000000000000LL
#define mod 998244353LL
//#define mod 1000000007LL
#define eps 0.000000001
random_device rnd;// 非決定的な乱数生成器
mt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード

//#include<boost/multiprecision/cpp_int.hpp>
//#define bbi boost::multiprecision::cpp_int
//#include<atcoder/lazysegtree>


//整数同士の累乗の計算をする。
ll power(ll A, ll B) {
	ll result = 1;
	for (ll i=0;i<B;i++){
		result *= A;
	}
	return result;
}

// nのk乗をmodで割った余りを計算
ll power_mod(ll n, ll k){
	long long result = 1;
	while (k > 0){
		if ((k&1) ==1)result=(result*n)%mod;
		n=n*n%mod;
		k >>= 1;
	}
	return result;
}


//受け取った2次元文字の外側に、文字pをコーティングする。
vector<string> pad(vector<string> &s,char p){
	ll h=s.size();
	ll w=s[0].size();
	vector<string> res(h+2,string(w+2,p));
	rep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];
	return res;
}

// Union-Find
struct UnionFind {
	vector<int> par, siz;
	UnionFind(int n) : par(n, -1) , siz(n, 1) { }
	// 根を求める
	int root(int x) {
		if (par[x] == -1) return x;
		else return par[x] = root(par[x]);
	}
	// x と y が同じグループに属するかどうか (根が一致するかどうか)
	bool issame(int x, int y) {
		return root(x) == root(y);
	}
	// x を含むグループと y を含むグループとを併合する
	bool unite(int x, int y) {
		x = root(x), y = root(y);
		if (x == y) return false; 
		if (siz[x] < siz[y]) swap(x, y);
		par[y] = x;
		siz[x] += siz[y];
		return true;
	}
	// x を含むグループのサイズ
	int size(int x) {
		return siz[root(x)];
	}
};
template <typename T>
struct offset_vector {
    vector<T> data;
    const int offset;

    /** @brief 負のインデックスをサポートするベクトル
     * @param n 正のインデックスのサイズ
     * @param offset 負のインデックスに拡張する数
     * @param init_val 初期値（デフォルトはT()）
     */
    offset_vector(int n, int offset = 0, T init_val = T()) : offset(offset) {
        assert(0 <= n && "offset_vectorの初期化時のnは0以上でなければなりません");
        assert(0 <= offset && "offset_vectorの初期化時のoffsetは0以上でなければなりません");
        data.resize(n + offset, init_val);
    }

    T& operator[](int i) {
        assert(0 <= i + offset && "offset_vectorへの負方向の範囲外参照です");
        assert(i + offset < data.size() && "offset_vectorへの正方向の範囲外参照です");
        return data[i + offset];
    }

    auto begin() {
        return data.begin();
    }
    auto end() {
        return data.end();
    }
    auto rbegin() {
        return data.rbegin();
    }
    auto rend() {
        return data.rend();
    }
    auto size() const {
        return data.size();
    }
    size_t positive_size() const {
        return data.size() - offset;
    }
    auto empty() const {
        return data.empty();
    }
    auto front() {
        assert(!data.empty() && "offset_vectorが空です");
        return data.front();
    }
    auto back() {
        assert(!data.empty() && "offset_vectorが空です");
        return data.back();
    }

    void push_back(const T& value) {
        data.push_back(value);
    }
    void pop_back() {
        assert(offset < data.size() && "offset_vectorのpop_backは、負の要素に対しては使用できません");
        assert(!data.empty() && "offset_vectorのpop_backは、空のベクトルに対しては使用できません");
        data.pop_back();
    }

    friend ostream& operator<<(ostream& os, const offset_vector<T>& v) {
        for (size_t i = 0; i < v.size(); ++i) {
            if (i != 0) os << " ";
            os << v.data[i];
        }
        os << "\n";
        return os;
    }
};

//グリッド問題等用
vl dx={1,0,-1,0};
vl dy={0,1,0,-1};


ll solve(){
	ll n;
	cin>>n;
	if(n==0){return 1;}

	vl a(n);
	vl odd;
	vl even;
	ll allsum=0;
	rep(i,n){
		cin>>a[i];
		allsum+=a[i];
		if(a[i]%2==0)even.push_back(i);
		else odd.push_back(i);
	}

	if(odd.size()%2==1){
		cout<<"No"<<endl;
		return 0;
	}

	//1個でも偶数があればRotationで構築可能
	if(even.size()!=0){
		vl red(n);
		vl blue(n);
		while(even.size()>1){
			red[even.back()]=a[even.back()]/2;
			blue[even.back()]=a[even.back()]/2;
			even.pop_back();
		}
		odd.push_back(even[0]);

		vl abcd(odd.size());
		// abcd[0]を求める
		ll sums=0;
		rep(i,odd.size()){
			if(i%2==0){
				sums+=a[odd[i]];
			}else{
				sums-=a[odd[i]];
			}
		}
		abcd[0]=sums/2;
		rep(i,odd.size()-1){
			abcd[i+1]=a[odd[i]]-abcd[i];
		}

		rep(i,odd.size()){
			red[odd[i]]=abcd[i];
			blue[odd[i]]=abcd[(i+1)%odd.size()];
		}
		cout<<"Yes"<<endl;
		vdbg(red);
		vdbg(blue);
		return 0;
	}

	
	//奇数のみの場合
	vector<vector<offset_vector<pair<bool,bool>>>> dp(n+1,vector<offset_vector<pair<bool,bool>>>(n/2+1,offset_vector<pair<bool,bool>>(150*n+1,150*n,{false,false})));
	dp[0][0][0]={true,false};
	rep(i,n){
		rep(j,n/2){
			loop(k,-150*i,150*i){
				if(!dp[i][j][k].first)continue;

				//使わない場合
				dp[i+1][j][k]={true,false};

				//使う場合
				dp[i+1][j+1][k+a[i]]={true,true};
				
			}
		}
	}
	if(!dp[n][n/2][allsum/2].first){
		cout<<"No"<<endl;
		return 0;
	}

	vl g1;
	vl g2;
	ll nowj=n/2;
	ll nowk=allsum/2;
	rrep(i,n){
		if(dp[i+1][nowj][nowk].second){
			nowj--;
			nowk-=a[i];
			g1.push_back(i);
		}else{
			g2.push_back(i);
		}
	}
	//vdbg(g1);
	//vdbg(g2);

	vl red(n);
	vl blue(n);
	vl abcd(n);
	abcd[0]=0;

	rep(i,n-1){
		if(i%2==0){
			abcd[i+1]=a[g1[i/2]]-abcd[i];
		}else{
			abcd[i+1]=a[g2[i/2]]-abcd[i];
		}
	}

	rep(i,n){
		if(i%2==0){
			red[g1[i/2]]=abcd[i];
			blue[g1[i/2]]=abcd[(i+1)%n];
		}else{
			red[g2[i/2]]=abcd[i];
			blue[g2[i/2]]=abcd[(i+1)%n];
		}
	}
	cout<<"Yes"<<endl;
	vdbg(red);
	vdbg(blue);

	return 0;
}

//メイン
int main(){
	while(solve()==0);
	return 0;
}
accuracy: 1
time_ms: 2850
memory_kb: 109544
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define vl vector<ll>
#define vll vector<ll>
#define rep(i,n) for(int i = 0;i < n;i++)
#define per(i,n) for(int i = n - 1;i >= 0;i--)
#define fore(e,v) for(auto &&e,v)
#define si(a) (int)(size(a))
#define all(a) begin(a),end(a)
#define lb(v,x) (lower_bound(all(v),x) - begin(v))
#define eb emplace_back

template<typename T , typename S> bool chmin(T &a, const S& b){
	return a > b ? a = b, 1:0;
}
template<typename T , typename S> bool chmax(T &a, const S& b){
	return a < b ? a = b, 1:0;
}

const ll inf = 3e18;

struct _{
	_(){
		cin.tie(0) -> sync_with_stdio(0),cout.tie(0);
	}
}__;

bool solve(ll n,vll s,vll &a, vll &b){
	ll sum = 0;
	{
		rep(i,n)sum += s[i];
		if(sum % 2 != 0)return false;
	}
	if(n % 2 == 1){
		ll sm = 0;
		rep(i,n){
			if(i % 2 == 0)sm += s[i];
			else sm -= s[i];
		}
		assert(sum % 2 == 0);
		ll x = sm / 2;
		a[0] = b[n - 1] = x;
		rep(i,n - 1){
			ll y = s[i] - x;
			a[i + 1] = b[i] = y;
			x = y;
		}
		// rep(i,n)assert(s[i] == a[i] + b[i]);
		// assert(s[n - 1] == a[n - 1] + b[n - 1]);
		return true;
	}
	rep(i,n)if(s[i] % 2 == 0){
		vll s2;
		rep(j,n)if(j != i)s2.eb(s[j]);
		vll a2(n - 1),b2(n - 1);
		solve(n - 1,s2,a2,b2);
		ll k = 0;
		rep(j,n){
			if(i == j){
				a[j] = b[j] = s[j] / 2;
			}else{
				a[j] = a2[k];
				b[j] = b2[k];
				k++;
			}
		}
		return true;
	}
	
	vll s1,s2;
	{
		ll base = 150 * n  + 10;
		bool dp[n + 1][n + 1][2 * base];
		rep(i,n + 1)rep(j,n + 1)rep(k,2 * base)dp[i][j][k] = 0;
		dp[0][0][base] = 1;
		rep(i,n){
			rep(j,n){
				rep(sm,2 * base){
					if(dp[i][j][sm]){
						dp[i + 1][j][sm]  = 1;
						if(0 <= sm + s[i] && sm + s[i] < 2 * base)dp[i + 1][j + 1][sm + s[i]] = 1;
					}
				}
			}
		}
		if(dp[n][n / 2][base + sum / 2] == 0)return false;
		// cout<<"here"<<endl;
		// cout<<base + sum / 2<<" "<<n<<" "<<n / 2<<" "<<dp[n][n / 2][base + sum / 2]<<endl;
		ll j = n / 2,sm = base + sum / 2;
		per(i,n){
			if(dp[i][j][sm]){
				s2.eb(i);
			}
			else{
				j--,sm -= s[i];
				s1.eb(i);
			}
		}
		// cout<<"s1:"; rep(i,n / 2)cout<<s1[i]<<" ";cout<<endl;
		// cout<<"s2:"; rep(i,n / 2)cout<<s2[i]<<" ";cout<<endl;
		assert(si(s1) == n / 2);
		assert(si(s2) == n / 2);
		{
			ll ss = 0;
			for(auto &&e : s1)ss += s[e];
			assert(ss == sum / 2);
		}
	}
	vll p(n);
	{
		rep(i,n / 2){
			p[i * 2] = s1[i];
			p[i * 2 + 1] = s2[i];
		}
	}

	ll x = 0;
	a[p[0]] = b[p[n - 1]] = x;
	rep(i,n - 1){
		ll y = s[p[i]] - x;
		a[p[i + 1]] = b[p[i]] = y;
		x = y;
	}
	rep(i,n)assert(s[i] == a[i] + b[i]);
	return true;
}

int main(){
	while(1){
		ll n;
		cin>>n;
		if(n == 0)break;
		vll s(n);
		rep(i,n)cin>>s[i];
		vll a(n),b(n);
		if(solve(n,s,a,b)){
			
			cout<<"Yes"<<endl;
			rep(i,n)cout<<a[i]<<" \n"[i == n - 1];
			rep(i,n)cout<<b[i]<<" \n"[i == n - 1];
			cout<<flush;

			// rep(i,n)assert(s[i] == a[i] + b[i]);
		}else{
			cout<<"No"<<endl;
		}

	}
}
accuracy: 1
time_ms: 1620
memory_kb: 107000
final_rank: 2

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

bool solving = true;

vector<bool> knapsack(vector<int> v) {
  const int n = v.size();
  const int L = 150 * 70 / 2;
  const int S = accumulate(v.begin(), v.end(), 0);
  const pair<int, int> INF = make_pair(-1, -1);
  vector dp(n + 1, vector(n / 2 + 1, vector<pair<int, int>>(2 * L + 1, INF)));
  dp[1][0][v[0] + L] = make_pair(0, v[0]);
  dp[1][1][-v[0] + L] = make_pair(1, -v[0]);
  for(int i = 1; i < n; i++) {
    for(int j = 0; j <= n / 2; j++) {
      for(int s = -L; s <= L; s++) {
        if(dp[i][j][s + L] == INF) continue;
        int sp = s + v[i], sm = s - v[i];
        if(-L <= sp && sp <= L) dp[i + 1][j][sp + L] = make_pair(0, v[i]);
        if(-L <= sm && sm <= L && j < n / 2) dp[i + 1][j + 1][sm + L] = make_pair(1, -v[i]);
      }
    }
  }
  if(dp[n][n / 2][0 + L].first == -1) {
    vector<bool> ret;
    return ret;
  }
  vector<bool> ret(n);
  for(int i = n, j = n / 2, s = 0; i > 0; i--) {
    ret[i - 1] = dp[i][j][s + L].first;
    int j2 = j - dp[i][j][s + L].first, s2 = s - dp[i][j][s + L].second;
    j = j2, s = s2;
  }
  return ret;
}

vector<pair<int, int>> construct(vector<int> v) {
  const int n = v.size();
  vector<pair<int, int>> ret(n);
  ret[0] = make_pair(0, v[0]);
  for(int i = 1; i < n; i++) {
    const int x = ret[i - 1].second;
    ret[i] = make_pair(x, v[i] - x);
  }
  assert(ret[n - 1].second % 2 == 0);
  const int k = ret[n - 1].second / 2;
  for(int i = 0; i < n; i++) {
    ret[i].first  += k * (i % 2 == 0 ? 1 : -1);
    ret[i].second += k * (i % 2 == 0 ? -1 : 1);
  }
  return ret;
}

void solve() {
  int n;
  cin >> n;
  if(n == 0) {
    solving = false;
    return;
  }
  int cnt_odd = 0;
  vector<int> s(n);
  for(int i = 0; i < n; i++) {
    cin >> s[i];
    if(abs(s[i]) % 2 == 1) cnt_odd++;
  }
  if(cnt_odd % 2 == 1) {
    cout << "No" << endl;
    return;
  }
  vector<int> ev, ei;
  vector<int> ov, oi;
  for(int i = 0; i < n; i++) {
    if(abs(s[i]) % 2 == 0) {
      ev.push_back(s[i]);
      ei.push_back(i);
    }else {
      ov.push_back(s[i]);
      oi.push_back(i);
    }
  }
  if(cnt_odd == n) {
    vector<bool> res = knapsack(ov);
    if(res.empty()) {
      cout << "No" << "\n";
      return;
    }
    vector<int> v(ov.size()), id(oi.size());
    for(int i = 0, p = 0, q = 1; i < n; i++) {
      if(res[i]) {
        v[p] = ov[i], id[p] = oi[i], p += 2;
      }else {
        v[q] = ov[i], id[q] = oi[i], q += 2;
      }
    }
    vector<int> a(n), b(n);
    vector<pair<int, int>> res2 = construct(v);
    for(int i = 0; i < ov.size(); i++) {
      a[id[i]] = res2[i].first, b[id[i]] = res2[i].second;
    }
    for(int i = 0; i < ev.size(); i++) {
      a[ei[i]] = b[ei[i]] = s[ei[i]] / 2;
    }
    cout << "Yes" << "\n";
    for(int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n";
    for(int i = 0; i < n; i++) cout << b[i] << " "; cout << "\n";
  }else {
    vector<int> a(n), b(n);
    {
      vector<int> v = ov;
      v.push_back(ev[0]);
      vector<pair<int, int>> res = construct(v);
      for(int i = 0; i < ov.size(); i++) {
        a[oi[i]] = res[i].first, b[oi[i]] = res[i].second;
      }
      {
        a[ei[0]] = res.back().first, b[ei[0]] = res.back().second;
      }
    }
    {
      for(int i = 1; i < ev.size(); i++) {
        a[ei[i]] = b[ei[i]] = ev[i] / 2;
      }
    }
    cout << "Yes" << "\n";
    for(int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n";
    for(int i = 0; i < n; i++) cout << b[i] << " "; cout << "\n";
  }
}

int main() {
  while(solving) solve();
}
accuracy: 1
time_ms: 3790
memory_kb: 215808
final_rank: 7

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

using i64 = long long;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n;
    
    auto solve = [&]() {
        vector<int> s(n), a(n), b(n);
        for (int i = 0; i < n; i++) {
            cin >> s[i];
        }

        vector pos(2, vector<int>());
        for (int i = 0; i < n; i++) {
            pos[abs(s[i]) % 2].push_back(i);
        }

        if (pos[1].size() % 2 == 1) {
            cout << "No\n";
            return;
        } else if (!pos[0].empty()) {
            int e = pos[0].back();
            pos[0].pop_back();
            pos[1].push_back(e);
            for (int i : pos[0]) {
                a[i] = b[i] = s[i] / 2;
            }
            int z = 0;
            for (int i : pos[1]) {
                z += s[i];
            }
            assert(z % 2 == 0);
            z /= 2;
            int sz = pos[1].size();
            for (int i = 0; i < sz; i++) {
                if (i == 0) {
                    for (int k = 0; k < sz; k += 2) {
                        a[pos[1][i]] += s[pos[1][k]];
                    }
                    a[pos[1][i]] -= z;
                } else {
                    a[pos[1][i]] = s[pos[1][i - 1]] - a[pos[1][i - 1]];
                }
            }
            for (int i = 0; i < sz; i++) {
                b[pos[1][i]] = a[pos[1][(i + 1) % sz]];
            }
        } else {
            int sz = pos[1].size();
            const int V = 150, S = V * sz / 2;
            vector dp(sz + 1, vector(sz + 1, vector<int>(2 * S + 1)));
            dp[0][0][S] = true;
            for (int i = 0; i < sz; i++) {
                int v = s[pos[1][i]];
                for (int j = 0; j <= i; j++) {
                    for (int k = 0; k <= 2 * S; k++) {
                        if (dp[i][j][k]) {
                            if (0 <= k + v && k + v <= 2 * S) {
                                dp[i + 1][j + 1][k + v] = true;
                            }
                            if (0 <= k - v && k - v <= 2 * S) {
                                dp[i + 1][j][k - v] = true;
                            }
                        }
                    }
                }
            }
            if (!dp[sz][sz / 2][S]) {
                cout << "No\n";
                return;
            }
            vector part(2, vector<int>());
            for (int i = sz - 1, j = sz / 2, k = S; i >= 0; i--) {
                int v = s[pos[1][i]];
                if (0 <= k - v && k - v <= 2 * S && j > 0 && dp[i][j - 1][k - v]) {
                    j--;
                    k -= v;
                    part[0].push_back(pos[1][i]);
                } else if (0 <= k + v && k + v <= 2 * S && dp[i][j][k + v]) {
                    k += v;
                    part[1].push_back(pos[1][i]);
                } else {
                    assert(false);
                }
            }
            assert(part[0].size() == part[1].size() && part[0].size() == sz / 2);
            for (int i = 0; i < sz; i++) {
                pos[1][i] = part[i % 2][i / 2];
            }
            for (int i = 1; i < sz; i++) {
                a[pos[1][i]] = s[pos[1][i - 1]] - a[pos[1][i - 1]];
            }
            for (int i = 0; i < sz; i++) {
                b[pos[1][i]] = a[pos[1][(i + 1) % sz]];
            }
        }

        cout << "Yes\n";
        for (int i = 0; i < n; i++) {
            cout << a[i] << " \n"[i + 1 == n];
        }
        for (int i = 0; i < n; i++) {
            cout << b[i] << " \n"[i + 1 == n];
        }
        #ifdef LOCAL
        for (int i = 0; i < n; i++) {
            assert(a[i] + b[i] == s[i]);
        }
        assert(multiset<int>(a.begin(), a.end()) == multiset<int>(b.begin(), b.end()));
        #endif
    };

    while (cin >> n && n != 0) {
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 3320
memory_kb: 213016
final_rank: 6

code_snippet: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
#undef long
#define long long long
#define vec vector
template <typename T>
ostream &operator<<(ostream &os, vector<T> &a)
{
    const int n = a.size();
    rep(i, n)
    {
        os << a[i];
        if (i + 1 != n)
            os << " ";
    }
    return os;
}
void solve()
{
    int n;
    cin >> n;
    if (n == 0)
        exit(0);
    vec<long> s(n);
    rep(i, n) cin >> s[i];
    long s_sowa = 0;
    for (long i : s)
        s_sowa += i;
    if (s_sowa % 2 != 0)
    {
        cout << "No" << endl;
        return;
    }
    auto f = [](vec<long> a) -> pair<vec<long>, vec<long>>
    {
        int n = a.size();
        vec<long> x(n), y(n);
        x[0] = 0;
        y[0] = a[0] - x[0];
        for (int i = 1; i < n; i++)
        {
            x[i] = y[i - 1];
            y[i] = a[i] - x[i];
        }
        x[0] = y[n - 1] / 2;
        y[0] = a[0] - x[0];
        for (int i = 1; i < n; i++)
        {
            x[i] = y[i - 1];
            y[i] = a[i] - x[i];
        }
        assert(x[0] == y[n - 1]);
        return {x, y};
    };
    if (n % 2 == 1)
    {
        auto [a, b] = f(s);
        cout << "Yes" << endl;
        cout << a << endl;
        cout << b << endl;
        return;
    }
    int gu_idx = -1;
    rep(i, n) if (s[i] % 2 == 0) gu_idx = i;
    if (gu_idx != -1)
    {
        vec<long> ss;
        rep(i, n) if (i != gu_idx) ss.push_back(s[i]);
        auto [aa, bb] = f(ss);
        vec<long> a, b;
        rep(i, n)
        {
            if (i == gu_idx)
            {
                a.push_back(s[i] / 2);
                b.push_back(s[i] / 2);
            }
            else
            {
                a.push_back(aa[i - (i > gu_idx)]);
                b.push_back(bb[i - (i > gu_idx)]);
            }
        }
        cout << "Yes" << endl;
        cout << a << endl;
        cout << b << endl;
        return;
    }
    vec<vec<vec<bool>>> dd(n + 1, vec<vec<bool>>(n / 2 + 1, vec<bool>(2 * n * 150 + 1)));
    dd[0][0][n * 150] = true;
    rep(j, n)
    {
        for (int i = n / 2; i >= 0; i--)
        {
            rep(k, 2 * n * 150 + 1)
            {
                if (!dd[j][i][k])
                    continue;
                dd[j + 1][i][k] = true;
            }
            if (i != n / 2)
            {
                rep(k, 2 * n * 150 + 1)
                {
                    if (!dd[j][i][k])
                        continue;
                    int to = k + s[j];
                    if (to < 0 || to >= 2 * n * 150 + 1)
                        continue;
                    dd[j + 1][i + 1][to] = true;
                }
            }
        }
    }
    const long s_half = s_sowa / 2;
    if (!dd[n][n / 2][s_half + n * 150])
    {
        cout << "No" << endl;
        return;
    }
    vec<int> soe, soe2;
    int now_idx = n / 2;
    int now_sum = s_half + n * 150;
    for (int i = n - 1; i >= 0; i--)
    {
        if (now_idx >= 1 && dd[i][now_idx - 1][now_sum - s[i]])
        {
            soe.push_back(i);
            now_idx--;
            now_sum -= s[i];
        }
        else
        {
            soe2.push_back(i);
        }
    }
    assert(now_sum == n * 150);
    assert(now_idx == 0);
    assert(soe.size() == soe2.size());
    assert(soe.size() == n / 2);
    vec<long> ss;
    rep(i, n / 2)
    {
        ss.push_back(s[soe[i]]);
        ss.push_back(s[soe2[i]]);
    }
    auto [aa, bb] = f(ss);
    vec<long> a(n), b(n);
    rep(i, n / 2)
    {
        a[soe[i]] = aa[2 * i];
        b[soe[i]] = bb[2 * i];
        a[soe2[i]] = aa[2 * i + 1];
        b[soe2[i]] = bb[2 * i + 1];
    }
    cout << "Yes" << endl;
    cout << a << endl;
    cout << b << endl;
    auto aaa = a, bbb = b;
    rep(i, n) assert(aaa[i] + bbb[i] == s[i]);
    sort(aaa.begin(), aaa.end()), sort(bbb.begin(), bbb.end());
    rep(i, n) assert(aaa[i] == bbb[i]);
    return;
}
int main()
{
    while (true)
        solve();
}
accuracy: 1
time_ms: 3820
memory_kb: 9944
final_rank: 3