Problem:
Missing Numbers
Toshizo is the manager of a convenience store chain in Hakodate.
Every day, each of the stores in his chain sends him a table of the
products that they have sold.  Toshizo's job is to compile these
figures and calculate how much the stores have sold in total.
The type of a table that is sent to Toshizo by the stores looks like this
(all numbers represent units of products sold):
Store1      Store2      Store3         Totals
 Product A                 -5          40          70      |    105
 Product B                 20          50          80      |    150
 Product C                 30          60          90      |    180
 -----------------------------------------------------------
 Store's total sales       45         150         240           435
By looking at the table, Toshizo can tell at a glance which goods are
selling well or selling badly, and which stores are paying well and
which stores are too empty. Sometimes, customers will bring products
back, so numbers in a table can be negative as well as positive.
Toshizo reports these figures to his boss in Tokyo, and together they
sometimes decide to close stores that are not doing well and sometimes
decide to open new stores in places that they think will be profitable.
So, the total number of stores managed by Toshizo is not fixed. Also,
they often decide to discontinue selling some products that are not
popular, as well as deciding to stock new items that are likely to be
popular. So, the number of products that Toshizo has to monitor is also
not fixed.
One New Year, it is very cold in Hakodate. A water pipe bursts in
Toshizo's office and floods his desk. When Toshizo comes to work, he
finds that the latest sales table is not legible. He can make out some
of the figures, but not all of them. He wants to call his boss in
Tokyo to tell him that the figures will be late, but he knows that his
boss will expect him to reconstruct the table if at all possible.
Waiting until the next day won't help, because it is the New Year, and
his shops will be on holiday.  So, Toshizo decides either to work out
the values for himself, or to be sure that there really is no unique
solution. Only then can he call his boss and tell him what has
happened.
But Toshizo also wants to be sure that a problem like this never
happens again. So he decides to write a computer program that all the
managers in his company can use if some of the data goes missing
from their sales tables. For instance, if they have a table like:
Store 1     Store 2    Store 3            Totals
 Product A              ?           ?         70        |     105
 Product B              ?          50          ?        |     150
 Product C             30          60         90        |     180
 --------------------------------------------------------
 Store's total sales   45         150        240              435
then Toshizo's program will be able to tell them the correct figures
to replace the question marks.
In some cases, how
...(truncated)

Submitted solutions:

code_snippet: #include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct dat {
	int px, py; string s;
};
bool operator<(const dat& d1, const dat& d2) {
	if (d1.px != d2.px) return d1.px < d2.px;
	return d1.py < d2.py;
}
int H, W, z; string s[109][109];
int main() {
	while (cin >> H, H) {
		cin >> W; H++; W++;
		if (z++) cout << endl;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				cin >> s[i][j];
			}
		}
		vector<dat> ret;
		for (int i = 0; i <= 1000; i++) {
			for (int j = 0; j < H - 1; j++) {
				int a = stoi(s[j][W - 1]), cnt = 0;
				for (int k = 0; k < W - 1; k++) {
					if (s[j][k] != "?") a -= stoi(s[j][k]);
					else cnt++;
				}
				if (cnt == 1) {
					for (int k = 0; k < W - 1; k++) {
						if (s[j][k] == "?") {
							s[j][k] = to_string(a);
							ret.push_back(dat{ j, k, s[j][k] });
						}
					}
				}
			}
			for (int j = 0; j < W - 1; j++) {
				int a = stoi(s[H - 1][j]), cnt = 0;
				for (int k = 0; k < H - 1; k++) {
					if (s[k][j] != "?") a -= stoi(s[k][j]);
					else cnt++;
				}
				if (cnt == 1) {
					for (int k = 0; k < H - 1; k++) {
						if (s[k][j] == "?") {
							s[k][j] = to_string(a);
							ret.push_back(dat{ k, j, s[k][j] });
						}
					}
				}
			}
		}
		bool flag = true;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (s[i][j] == "?") flag = false;
			}
		}
		if (!flag) cout << "NO" << endl;
		else {
			sort(ret.begin(), ret.end());
			for (int i = 0; i < ret.size(); i++) cout << ret[i].s << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 640
memory_kb: 3652
final_rank: 20

code_snippet: #include<iostream>
#include<string>
#include<vector>
using namespace std;
int H, W, a[101][11], c;
int main() {
	while (true) {
		cin >> H; if (H == 0)break; if (c)cout << endl; cin >> W;
		vector<pair<int, int>>v;
		for (int i = 0; i <= H; i++) {
			for (int j = 0; j <= W; j++) {
				string S; cin >> S; if (S == "?") { a[i][j] = -(1 << 30); v.push_back(make_pair(i, j)); }
				else a[i][j] = stoi(S);
			}
		}
		for (int i = 0; i <= H*W; i++) {
			for (int j = 0; j < W; j++) {
				int f = 0, g = 0, h = 0;
				for (int k = 0; k < H; k++) { if (a[k][j] == -(1 << 30)) { g++; h = k; } else { f += a[k][j]; } }
				if (g == 1) { a[h][j] = a[H][j] - f; }
			}
			for (int j = 0; j < H; j++) {
				int f = 0, g = 0, h = 0;
				for (int k = 0; k < W; k++) { if (a[j][k] == -(1 << 30)) { g++; h = k; } else { f += a[j][k]; } }
				if (g == 1) { a[j][h] = a[j][W] - f; }
			}
		}
		bool flag = true; for (int i = 0; i <= H; i++) { for (int j = 0; j <= W; j++) { if (a[i][j] == -(1 << 30))flag = false; } }
		if (flag == false) cout << "NO" << endl;
		else { for (pair<int, int> i : v)cout << a[i.first][i.second] << endl; }
		c++;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 2956
final_rank: 17

code_snippet: #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;

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

typedef long long ll;

#define EPS (1e-8)

const int q = -999999999;

typedef long long ll;

typedef vector<ll> vec;
typedef vector<vec> mat;

vec gauss_jordan(mat& A, vec& b) {
  int n = A.size();
  mat B(n, vec(n+1));
  rep(i, n) rep(j, n) B[i][j] = A[i][j];
  rep(i, n) B[i][n] = b[i];

  rep(i, n) {
    int pivot = i;
    REP(j, i, n) {
      if(abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
    }
    swap(B[i], B[pivot]);

    if(abs(B[i][i]) < EPS) return vec();

    REP(j, i+1, n+1) B[i][j] /= B[i][i];
    rep(j, n) {
      if(i != j) {
        REP(k, i+1, n+1) B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }

  vec x(n);
  rep(i, n) x[i] = B[i][n];
  return x;
}

int N, M;
mat in;
vector<pair<int, int>> qpos;

#define fail { cout << "NO\n"; goto next; }

int main() {

  bool first = 0;
  while(cin >> N >> M && N) {
    if(first) cout << endl;
    first = 1;
    bool remain = 0;
    in.clear(); in.resize(N + 1); rep(i, N + 1) in[i].resize(M + 1);
    qpos.clear();
    rep(i, N + 1) rep(j, M + 1) {
      string s; cin >> s;
      if(s == "?") in[i][j] = q, qpos.emplace_back(i, j);
      else in[i][j] = stoi(s);
    }

    rep(_, (N + 1) * (M + 1) + 10) {
    {
      rep(i, N + 1) {
        int xcnt = 0;
        ll sum = 0;
        int lastq = -1;
        rep(j, M) {
          if(in[i][j] == q) xcnt ++, lastq = j;
          else sum += in[i][j];
        }
        if(xcnt == 0) {
          if(in[i][M] == q) in[i][M] = sum;
          else if(in[i][M] != sum) fail;
        }
        else if(xcnt == 1) {
          if(in[i][M] == q) {}
          else in[i][lastq] = in[i][M] - sum;
        }
        else {}
      }
    }

    {
      rep(j, M + 1) {
        int xcnt = 0;
        ll sum = 0;
        int lastq = -1;
        rep(i, N) {
          if(in[i][j] == q) xcnt ++, lastq = i;
          else sum += in[i][j];
        }
        if(xcnt == 0) {
          if(in[N][j] == q) in[N][j] = sum;
          else if(in[N][j] != sum) fail;
        }
        else if(xcnt == 1) {
          if(in[N][j] == q) {}
          else in[lastq][j] = in[N][j] - sum;
        }
        else {}
      }
    }}

    rep(i, N + 1) rep(j, M + 1) {
      remain |= in[i][j] == q;
    }

    if(remain) fail;

    for(auto e: qpos) {
      cout << in[e.first][e.second] << endl;
    }

  next:;
  }
  
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3156
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MAX 105
#define INF (1e9)

int s2i(string s){
  stringstream ss;
  int res;
  ss<<s;
  ss>>res;
  return res;
}


int a[MAX][MAX];
vector<int> vx,vy;
int H,W;
string str;

bool solve(int id){
  int y=vy[id],x=vx[id];
  int ch=0,cw=0,sh=0,sw=0;
  for(int i=0;i<H;i++){
    if(a[i][x]==INF)continue;
    ch++;
    sh+=a[i][x];
  }
  if(ch==H-1){
    a[y][x]=a[H][x]-sh;
    return true;
  }

  for(int i=0;i<W;i++){
    if(a[y][i]==INF)continue;
    cw++;
    sw+=a[y][i];
  }
  if(cw==W-1){
    a[y][x]=a[y][W]-sw;
    return true;
  }
  return false;
}

int main(){
  int Tc=0;
  while(1){
    
    cin>>H;
    if(H==0)break;
    if(Tc!=0)cout<<endl;
    Tc++;

    cin>>W;
    vx.clear();
    vy.clear();
    for(int i=0;i<=H;i++){
      for(int j=0;j<=W;j++){
        cin>>str;
        if(str=="?"){
          a[i][j]=INF;
          vy.push_back(i);
          vx.push_back(j);
        }else a[i][j]=s2i(str);
      }
    }
    bool update=true;
    while(update){
      update=false;
      for(int i=0;i<(int)vx.size();i++){
        if(a[vy[i]][vx[i]]==INF)
          update|=solve(i);
      }
    }
    vector<int> ans;
    for(int i=0;i<(int)vx.size();i++)
      if(a[vy[i]][vx[i]]!=INF)
        ans.push_back(a[vy[i]][vx[i]]);
    if(ans.size()!=vx.size())cout<<"NO"<<endl;
    else{
      for(int i=0;i<(int)ans.size();i++)cout<<ans[i]<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1296
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF=1<<30;
typedef pair<int,int> P;
typedef pair<P,int> PP;
int main() {
  int n,m;
  bool ff=0;
  while(cin >> n && n) {
    cin >> m;
    if(ff) cout << endl;
    ff=1;
    int a[n+1][m+1],cnt=0;
    for(int i=0; i<=n; i++) {
      for(int j=0; j<=m; j++) {
        string s;
        cin >> s;
        if(s[0]=='?') {
          a[i][j]=INF;
          cnt++;
        } else {
          stringstream ss;
          ss << s;
          ss >> a[i][j];
        }
      }
    }
    vector<PP> ans;
    while(1) {
      bool ck=0;
      for(int i=0; i<n; i++) {
        for(int j=0; j<m; j++) {
          if(a[i][j]==INF) {
            bool f=1;
            for(int k=0; k<m; k++) {
              if(j!=k&&a[i][k]==INF) f=0;
            }
            if(f) {
              int sum=a[i][m];
              for(int k=0; k<m; k++) {
                if(j!=k) sum-=a[i][k];
              }
              a[i][j]=sum;
              ans.push_back(PP(P(i,j),a[i][j]));
              ck=1;
              continue;
            }
            f=1;
            for(int k=0; k<n; k++) {
              if(i!=k&&a[k][j]==INF) f=0;
            }
            if(f) {
              int sum=a[n][j];
              for(int k=0; k<n; k++) {
                if(i!=k) sum-=a[k][j];
              }
              a[i][j]=sum;
              ans.push_back(PP(P(i,j),a[i][j]));
              ck=1;
            }
          }
        }
      }
      if(!ck) break;
    }
    if(ans.size()!=cnt) cout << "NO" << endl;
    else {
      sort(ans.begin(),ans.end());
      for(int i=0; i<ans.size(); i++) cout << ans[i].second << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1228
final_rank: 6

code_snippet: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int INF = 1e9;

int H, W;
bool f;

void check(int y, int x, int py, int px, int total, vector< vector<int> > &v, vector<int> &h, vector<int> &w){
  int sum = 0, ty, tx;
  while(y < H - 1 && x < W - 1){
    if(v[y][x] == INF) { ty = y; tx = x; }
    else sum += v[y][x];
    y += py; x += px;
  }
  v[ty][tx] = total - sum;
  h[ty]--; w[tx]--;
}

vector< vector<int> > solve(vector< vector<int> > v, vector<int> &h, vector<int> &w){
  int cnt = 0;
  REP(i, H) REP(j, W) if(v[i][j] == INF) ++cnt;
  while(cnt != 0){
    bool flg = false;
    REP(i, H) if(h[i] == 1) { check(i, 0, 0, 1, v[i][W - 1], v, h, w); --cnt; flg = true; }
    REP(i, W) if(w[i] == 1) { check(0, i, 1, 0, v[H - 1][i], v, h, w); --cnt; flg = true; }
    if(!flg) { return vector< vector<int> >(); }
  }
  f = true;
  return v;
}

int main() {
  bool t = false;
  while(cin >>H && H){
    cin >>W;
    ++H; ++W;
    f = false;
    vector< vector<int> > v(H, vector<int>(W));
    vector<int> w(W, 0), h(H, 0);
    REP(i, H){
      REP(j, W){
        string s; cin >>s;
        if(s != "?") v[i][j] = atoi(s.c_str());
        else { v[i][j] = INF; h[i]++; w[j]++; }
      }
    }
    if(t) cout <<endl;
    t = true;
    vector< vector<int> > ans = solve(v, h, w);
    if(f) { REP(i, H) REP(j, W) if(v[i][j] == INF) cout <<ans[i][j] <<endl; }
    else cout <<"NO" <<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1264
final_rank: 11

code_snippet: #include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
const double EPS=1e-8;
typedef vector<double>vec;
typedef vector<vec>mat;
inline double ABS(double a){return max(a,-a);}
vec gauss_jordan(const mat &A,const vec &b){
	int n=A.size();
	int m=A[0].size();
	mat B(n,vec(m+1));
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)B[i][j]=A[i][j];
	for(int i=0;i<n;i++)B[i][m]=b[i];
	for(int i=0;i<m;i++){
		int pivot=i;
		for(int j=i;j<n;j++){
			if(ABS(B[j][i])>ABS(B[pivot][i]))pivot=j;
		}
		swap(B[i],B[pivot]);
		if(ABS(B[i][i])<EPS){
			return vec();
		}
		for(int j=i+1;j<=m;j++)B[i][j]/=B[i][i];
		for(int j=0;j<n;j++){
			if(i!=j){
				for(int k=i+1;k<=m;k++)B[j][k]-=B[j][i]*B[i][k];
			}
		}
	}
	for(int i=m;i<n;i++)if(ABS(B[i][m])>EPS)return vec();
	vec x(m);
	for(int i=0;i<m;i++)x[i]=B[i][m];
	return x;
}
char in[1000];
int q[110][20];
int p[110][20];
int r[110];
int c[110];
int main(){
	int a,b;
	bool fi=false;
	while(scanf("%d",&a),a){
		if(fi)printf("\n");
		fi=true;
		scanf("%d",&b);
		for(int i=0;i<a;i++)r[i]=0;
		for(int i=0;i<b;i++)c[i]=0;
		int t=0;
		for(int i=0;i<a;i++)for(int j=0;j<b;j++)q[i][j]=-1;
		for(int i=0;i<a+1;i++)for(int j=0;j<b+1;j++){
			scanf("%s",in);
			if(in[0]=='?'){
				q[i][j]=t++;
				r[i]=1;
				c[j]=1;
			}else{
				sscanf(in,"%d",&p[i][j]);
			}
		}
		if(t>a+b){
			printf("NO\n");continue;
		}
		int sk=0;
		for(int i=0;i<a;i++)sk+=r[i];
		for(int i=0;i<b;i++)sk+=c[i];
		mat A(sk,vec(t));
		vec B(sk);
		int ind=0;
		for(int i=0;i<a;i++){
			if(!r[i])continue;
			B[ind]=p[i][b];
			for(int j=0;j<b;j++){
				if(~q[i][j])A[ind][q[i][j]]=1;
				else B[ind]-=p[i][j];
			}
			ind++;
		}
		for(int i=0;i<b;i++){
			if(!c[i])continue;
			B[ind]=p[a][i];
			for(int j=0;j<a;j++){
				if(~q[j][i])A[ind][q[j][i]]=1;
				else B[ind]-=p[j][i];
			}
			ind++;
		}
		vec x=gauss_jordan(A,B);
		if(x.size()==0){
			printf("NO\n");continue;
		}
		for(int i=0;i<x.size();i++)printf("%.0f\n",x[i]);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1248
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cassert>

using namespace std;

constexpr int UNKNOWN = (1 << 25);

void solve(vector<vector<int>> &matrix) {
  const int h = matrix.size();
  const int w = matrix[0].size();

  vector<vector<bool>> is_question(h - 1, vector<bool>(w - 1));
  vector<pair<int, int>> questions; // r, c
  vector<int> num_qr(h - 1, 0), num_qc(w - 1, 0);
  vector<int> sum_r(h - 1, 0), sum_c(w - 1, 0);

  for(int i = 0; i < h - 1; ++i) {
    sum_r[i] += matrix[i][w - 1];
  }

  for(int i = 0; i < w - 1; ++i) {
    sum_c[i] += matrix[h - 1][i];
  }

  for(int i = 0; i < h - 1; ++i) {
    for(int j = 0; j < w - 1; ++j) {
      if(matrix[i][j] == UNKNOWN) {
	questions.emplace_back(i, j);
	++num_qr[i];
	++num_qc[j];
	is_question[i][j] = true;
      }
      else {
	sum_r[i] -= matrix[i][j];
	sum_c[j] -= matrix[i][j];
      }
    }
  }

  while(!questions.empty()) {
    bool unloop = true;
    int i = 0;

    while(i < questions.size()) {
      const int r = questions[i].first;
      const int c = questions[i].second;

      int value = UNKNOWN;
      if(num_qr[r] == 1) {
	value = sum_r[r];
      }
      else if(num_qc[c] == 1) {
	value = sum_c[c];
      }

      if(value == UNKNOWN) {
	++i;
	continue;
      }

      matrix[r][c] = value;
      sum_r[r] -= value;
      sum_c[c] -= value;
      --num_qr[r];
      --num_qc[c];
      unloop = false;
      questions.erase(questions.begin() + i);
    }

    if(unloop) break;
  }

  if(questions.empty()) { // unique answer
    for(int i = 0; i < h - 1; ++i) {
      for(int j = 0; j < w - 1; ++j) {
	if(is_question[i][j]) {
	  cout << matrix[i][j] << endl;
	}
      }
    }
  }
  else {
    cout << "NO" << endl;
  }
  //cout << endl;
}

int main(){
  bool first = true;
  for(int p, s; cin >> p >> s && p;) {
    if(!first) cout << endl;
    first = false;

    vector<vector<int>> matrix(p + 1, vector<int>(s + 1));
    for(auto &row : matrix) {
      for(auto &e : row) {
	string input;
	cin >> input;
	e = (input == "?" ? UNKNOWN : stoi(input));
      }
    }

    solve(matrix);
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1260
final_rank: 10

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

const int INF = 1<<28;

const int MAXN = 105;
int R, C;
int G[MAXN][MAXN];

int getValue(int i, int j, int di, int dj) {
  int sum = 0;
  for(int d = -1; d <= 1; d += 2) {
    int ni = i;
    int nj = j;
    for(ni = i, nj = j;
        0 <= ni && ni < R && 0 <= nj && nj < C;
        ni += d*di, nj += d*dj) {
      if(ni == i && nj == j) continue;
      if(G[ni][nj] == INF) return INF;
      sum -= G[ni][nj];
    }
    if(ni >= 0 && nj >= 0) sum += G[ni][nj];
  }
  return sum;
}

int main() {
  while(cin >> R && R) {
    cin >> C;
    for(int i = 0; i < R+1; ++i) {
      for(int j = 0; j < C+1; ++j) {
        string s;
        cin >> s;
        if(s == "?") {
          G[i][j] = INF;
        } else {
          G[i][j] = atoi(s.c_str());

        }
      }
    }
    queue<pair<int,int> > que;
    int n = 0;
    for(int i = 0; i < R; ++i) {
      for(int j = 0; j < C; ++j) {
        if(G[i][j] == INF) {
          que.push(make_pair(i,j));
          ++n;
        }
      }
    }
    vector<pair<pair<int,int>, int> > res;
    while(!que.empty()) {
      int i = que.front().first;
      int j = que.front().second;
      que.pop();
      if(G[i][j] != INF) continue;
      int u = getValue(i,j,1,0);
      int v = getValue(i,j,0,1);
      if(u != INF || v != INF) {
        G[i][j] = min(u,v);
        res.push_back(make_pair(make_pair(i,j),G[i][j]));
        for(int k = 0; k < C; ++k) {
          if(G[i][k] == INF) que.push(make_pair(i,k));
        }
        for(int k = 0; k < R; ++k) {
          if(G[k][j] == INF) que.push(make_pair(k,j));
        }
      }
    }

    static bool first = true;
    if(first) first = false;
    else cout << endl;
    if(res.size() != n) {
      cout << "NO" << endl;
    } else {
      sort(res.begin(), res.end());
      for(int i = 0; i < res.size(); ++i) {
        cout << res[i].second << endl;
      }
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1300
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<deque>
#include<sstream>
#include<cstdlib>
#include<cassert>
#define MAX 1000
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
using namespace std;
typedef pair<int,int> P;
int h,w;
int G[MAX][MAX];
int A[MAX][2];// 0->r, 1->c
P B[MAX][2];
int C[MAX][2];
int D[MAX][2];
 
P Bsearch(int a,int b)
{
  int Size = (b == 0?h:w);
  for(int i=0;i<Size;i++)
    {
      if(b == 0)
    {
      if(G[i][a] == -inf)return P(a,i);
    }
      else
    {
      if(G[a][i] == -inf)return P(i,a);
    }
    }
}
 
int main()
{
  int blank = false;
  while(true)
    {
      cin >> h;
      if(!h)break;
      cin >> w;
      rep(i,MAX)rep(j,2)A[i][j] = C[i][j] = 0,B[i][j] = P(-inf,-inf);
 
      vector<P> ans;
      int hatena = 0;
      rep(i,h)
    {
      rep(j,w)
        {
          string input;
          cin >> input;
          if(input[0] == '?')
        {
          G[i][j] = -inf;   
          A[j][0]++;
          B[j][0] = P(j,i);
          A[i][1]++;
          B[i][1] = P(j,i);
          ans.push_back(P(j,i));
        }
          else
        {
          G[i][j] = (atoi)(input.c_str());
          C[j][0] += G[i][j];
          C[i][1] += G[i][j];
        }
        }
      cin >> D[i][1];
    }
      rep(i,w+1)
    cin >> D[i][0];
     

      bool Fin = true;
      bool found = false;
 
      while(true)
    {
      Fin = true;
      found = false;
      rep(i,w)
        {
          if(A[i][0] >= 1)Fin = false;
          if(A[i][0] == 1)
        {
          //cout << "erase! " << B[i][0].first << "," << B[i][0].second << endl;
          found = true; 
          int value = D[i][0]-C[i][0];
          P p = Bsearch(i,0);
           C[p.first][0]+=value;
          C[p.second][1]+=value;
           A[p.first][0]--;
          A[p.second][1]--;
          assert( G[p.second][p.first] == -inf);
          G[p.second][p.first] = value;
        }
        }
      rep(i,h)
        {
          if(A[i][1] >= 1)Fin = false;
          if(A[i][1] == 1)
        {
          //cout << "erase! " << B[i][1].first << "," << B[i][1].second << endl;
          found = true; 
          int value = D[i][1]-C[i][1];
          P p = Bsearch(i,1);
           C[p.first][0]+=value;
          C[p.second][1]+=value;
           A[p.first][0]--;
          A[p.second][1]--;
          assert(G[p.second][p.first] == -inf);
          G[p.second][p.first] = value;
        }
        }
 
      if(!Fin && !found)
        break;
      if(Fin)break;
 
    }
      if(!blank)blank = true;
      else cout << endl;
      if(!Fin && !found)
        cout << "NO" << endl;
      else
        {
          rep(i,ans.size())
        cout << G[ans[i].second][ans[i].first] << endl;
 
         
        }
    }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1640
final_rank: 15

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

const int INF = 1<<29;

int toInt(string s){
  stringstream ss;
  ss << s;
  int a;
  ss >> a;
  return a;
}

int main(){
  int p,s;
  bool flag = false;
  while(cin >> p && p){
    if(flag) cout << endl;
    flag = true;
    cin >> s;
    s++,p++;
    vector<vector<int> >v;
    v.resize(p,vector<int>(s));
    //    cin.ignore();

    string str;
    vector<pair<int,int> >pos;
    int total = 0;
    int r_hatena[p];
    int c_hatena[s];
    int r[p];
    int c[s];

    fill(r_hatena,r_hatena+p,0);
    fill(c_hatena,c_hatena+s,0);
    fill(r,r+p,0);
    fill(c,c+s,0);

    for(int i=0; i<p; i++){
      for(int j=0; j<s; j++){
	cin >> str;
	if(str == "?"){
	  total++;
	  v[i][j] = INF;
	  pos.push_back(pair<int,int>(j,i));
	}
	else v[i][j] = toInt(str);
      }
    }


    for(int i=0; i<p; i++){
      int sum = 0;
      for(int j=0; j<s; j++){
	if(v[i][j] == INF) c_hatena[j]++;
	else if(j != s-1) sum += v[i][j];
      }
      r[i] = sum;
    }


    for(int i=0; i<s; i++){
      int sum = 0;
      for(int j=0; j<p; j++){
	if(v[j][i] == INF) r_hatena[j]++;
	else if(j != p-1) sum += v[j][i];
      }
      c[i] = sum;
    }


    priority_queue<pair<bool,int>,vector<pair<bool,int> >,
      greater<pair<bool,int> > >que;

    for(int i=0; i<s; i++){
      if(c_hatena[i] == 1){
	que.push(pair<bool,int>(true,i));
      }

    }

    for(int i=0; i<p; i++){
      if(r_hatena[i] == 1){
	que.push(pair<bool,int>(false,i));
      }
    }

    //p tate s yoko
    //0 is row 1 is column
    while(!que.empty()){
      pair<bool,int> tmp = que.top();
      que.pop();

      if(tmp.first == 0){
	if(r_hatena[tmp.second] <= 0)continue;

	for(int i=0; i<s; i++){
	  if(v[tmp.second][i] == INF){
	    v[tmp.second][i] = v[tmp.second][s-1]-r[tmp.second];
	    //	    cout << v[tmp.second][s-1] << endl;
	    //	    cout << r[tmp.second] << endl;
	    r_hatena[tmp.second]--;
	    c_hatena[i]--;
	    //	    cout << tmp.second << " " << i << endl;
	    total--;
	    c[i] += v[tmp.second][i];
	    if(c_hatena[i] == 1)
	      que.push(pair<bool,int>(1,i));
	  }
	}





      } else {
	if(c_hatena[tmp.second] <= 0)continue;
	for(int i=0; i<p; i++){
	  if(v[i][tmp.second] == INF){
	    //	    cout << i << " " << tmp.second << endl;
	    v[i][tmp.second] = v[p-1][tmp.second]-c[tmp.second];
	    c_hatena[tmp.second]--;
	    r_hatena[i]--;
	    //	    cout << i << " " << tmp.second << endl;
	    //	    cout << c_hatena[tmp.second] << endl;
	    //	    cout << r_hatena[i] << endl;
	    r[i] += v[i][tmp.second];
	    total--;
	    if(r_hatena[i] == 1)
	      que.push(pair<bool,int>(0,i));
	    //	    cout << v[i][tmp.second] << endl;
	  }
	}

      }






    }

    

    if(total <= 0){
      for(int i=0; i<pos.size(); i++){
	int x = pos[i].first;
	int y = pos[i].second;
	cout << v[y][x] << endl;
      }
    } else cout << "NO" << endl;
    //    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1268
final_rank: 13

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<deque>
#include<sstream>
#include<cstdlib>
#include<cassert>
#define MAX 1000
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
using namespace std;
typedef pair<int,int> P;
int h,w;
int G[MAX][MAX];
int A[MAX][2];// 0->r, 1->c
P B[MAX][2];
int C[MAX][2];
int D[MAX][2];

void printA()
{
  cout << "printA---" << endl;
  rep(i,w)cout << A[i][0] << " "; cout << endl;
  rep(i,h)cout << A[i][1] << " "; cout << endl;
  cout << endl;
}

void printB()
{
  cout << "printB---" << endl;
  rep(i,w)cout << B[i][0].first << "," << B[i][0].second << " "; cout << endl;
  rep(i,h)cout << B[i][1].first << "," << B[1][i].second << " "; cout << endl;
  cout << endl;
}

void printC()
{
  cout << "printC---" << endl;
  rep(i,w)cout << C[i][0] << " "; cout << endl;
  rep(i,h)cout << C[i][1] << " "; cout << endl;
  cout << endl;
}

void printD()
{
  cout << "printD---" << endl;
  rep(i,w)cout << D[i][0] << " "; cout << endl;
  rep(i,h)cout << D[i][1] << " "; cout << endl;
}

P Bsearch(int a,int b)
{
  int Size = (b == 0?h:w);
  for(int i=0;i<Size;i++)
    {
      if(b == 0)
	{
	  if(G[i][a] == -inf)return P(a,i);
	}
      else
	{
	  if(G[a][i] == -inf)return P(i,a);
	}
    }
}

int main()
{
  int blank = false;
  while(true)
    {
      cin >> h;
      if(!h)break;
      cin >> w;
      rep(i,MAX)rep(j,2)A[i][j] = C[i][j] = 0,B[i][j] = P(-inf,-inf);

      vector<P> ans;
      int hatena = 0;
      rep(i,h)
	{
	  rep(j,w)
	    {
	      string input;
	      cin >> input;
	      if(input[0] == '?')
		{
		  G[i][j] = -inf;   
		  A[j][0]++;
		  B[j][0] = P(j,i);
		  A[i][1]++;
		  B[i][1] = P(j,i);
		  ans.push_back(P(j,i));
		}
	      else 
		{
		  G[i][j] = (atoi)(input.c_str());
		  C[j][0] += G[i][j];
		  C[i][1] += G[i][j];
		}
	    }
	  cin >> D[i][1];
	}
      rep(i,w+1)
	cin >> D[i][0];
	
      //printA();
      //printB();
      //printC();
      //printD();
      //return 0;
      bool Fin = true;
      bool found = false;

      while(true)
	{
	  //cout << "phase !" << endl;
	  //printA();
	  //printB();
	  //printC();
	  //printD();
	  Fin = true;
	  found = false;
	  rep(i,w)
	    {
	      if(A[i][0] >= 1)Fin = false;
	      if(A[i][0] == 1)
		{
		  //cout << "erase! " << B[i][0].first << "," << B[i][0].second << endl;
		  found = true;	
		  int value = D[i][0]-C[i][0];
		  P p = Bsearch(i,0);
		   C[p.first][0]+=value;
		  C[p.second][1]+=value;
		   A[p.first][0]--;
		  A[p.second][1]--;
		  assert( G[p.second][p.first] == -inf);
		  G[p.second][p.first] = value;
		}
	    }
	  rep(i,h)
	    {
	      if(A[i][1] >= 1)Fin = false;
	      if(A[i][1] == 1)
		{
		  //cout << "erase! " << B[i][1].first << "," << B[i][1].second << endl;
		  found = true;	
		  int value = D[i][1]-C[i][1];
		  P p = Bsearch(i,1);
		   C[p.first][0]+=value;
		  C[p.second][1]+=value;
		   A[p.first][0]--;
		  A[p.second][1]--;
		  assert(G[p.second][p.first] == -inf);
		  G[p.second][p.first] = value;
		}
	    }

	  if(!Fin && !found)
	    break;
	  if(Fin)break;

	}
      if(!blank)blank = true;
      else cout << endl;
	  if(!Fin && !found)
	    cout << "NO" << endl;
	  else
	    {
	      rep(i,ans.size())
		cout << G[ans[i].second][ans[i].first] << endl;

		
	    }
    }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1648
final_rank: 16

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

int H, W;
string field[1010][1010];
string diff[1010][1010];
int totals[2][1010];
int total;

int toInt(string s){
  istringstream iss(s);
  int n;
  iss >> n;
  return n;
}

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

void Wcalc(int h, int x){
  int sum = 0;
  for(int j = 0 ; j < W ; j++){
    if(field[h][j] == "?") continue;
    else sum += toInt(field[h][j]);
  }
  /*
  cout << "x = " << x << ", y = " << h << endl;
  cout << "sum = " << sum << endl;
  cout << totals[0][h] << endl;
  */
  field[h][x] = toString(totals[0][h] - sum);
}

void Hcalc(int w, int y){
  int sum = 0;
  for(int i = 0 ; i < H ; i++){
    if(field[i][w] == "?") continue;
    else sum += toInt(field[i][w]);
  }
  /*
  cout << "x = " << w << ", y = " << y << endl;
  cout << totals[1][w] << endl;
  */
  field[y][w] = toString(totals[1][w] - sum);
}


int main(){
  bool flag = false;
  while(cin >> H){
    if(H == 0) break;
    if(flag) cout << endl;
    if(!flag) flag = true;
    
    cin >> W;
    
    
    int q = 0;
    
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
	cin >> field[i][j];
	diff[i][j] = field[i][j];
	if(field[i][j] == "?") q++;
      }
      cin >> totals[0][i];
    }
    
    for(int i = 0 ; i < W ; i++){
      cin >> totals[1][i];
    }
    cin >> total;
        
    bool cansolve = true;
    while(1){
      int tmp = q;
      for(int i = 0 ; i < H ; i++){
	int qcnt = 0;
	int w;
	for(int j = 0 ; j < W ; j++){
	  if(field[i][j] == "?") w = j, qcnt++;
	}
	if(qcnt == 1){
	  Wcalc(i, w);
	  q--;
	}
      }
      
      for(int j = 0 ; j < W ; j++){
	int qcnt = 0;
	int h;
	for(int i = 0 ; i < H ; i++){
	  if(field[i][j] == "?") h = i, qcnt++;
	}
	if(qcnt == 1){
	  Hcalc(j, h);
	  q--;
	}
      }
      
      if(q == 0) break;
      
      if(q == tmp){
	cansolve = false;
	break;
      }
    }
    
    if(!cansolve){
      cout << "NO" << endl;
      continue;
    }
    
    vector<string> ans;
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
	if(diff[i][j] == "?"){
	  ans.push_back(field[i][j]);
	}
      }
    }
    
    for(int i = 0 ; i < ans.size() ; i++){
      cout << ans[i] << endl;
    }
    /*
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
	cout << field[i][j] <<  ' ';
      }
      cout << endl;
    }
    cout << endl;
    */
  } 
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 17224
final_rank: 19

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <queue>
using namespace std;

typedef long long lli;

const int MAXR = 102;
const int MAXC = 12;
const lli INF = 1LL<<60;

int R, C;
lli G[MAXR][MAXC];
int numR[MAXR], numC[MAXC];

int main() {
  bool first = true;
  while(cin >> R && R) {
    cin >> C;
    vector<pair<int,int> > v;
    for(int i = 0; i < R+1; ++i) {
      for(int j = 0; j < C+1; ++j) {
        string s;
        cin >> s;
        if(s == "?") {
          G[i][j] = INF;
          v.push_back(make_pair(i,j));
        } else {
          G[i][j] = atoi(s.c_str());
        }
      }
    }

    fill(numR, numR+MAXR, 0);
    fill(numC, numC+MAXC, 0);
    for(int i = 0; i < R; ++i) {
      for(int j = 0; j < C; ++j) {
        if(G[i][j] == INF) {
          ++numR[i];
          ++numC[j];
        }
      }
    }

    queue<pair<int,int> > que;
    for(int i = 0; i < R; ++i) {
      for(int j = 0; j < C; ++j) {
        if(G[i][j] == INF) {
          if(numR[i] == 1 || numC[j] == 1) {
            que.push(make_pair(i,j));
          }
        }
      }
    }

    while(!que.empty()) {
      pair<int,int> p = que.front();
      que.pop();
      if(G[p.first][p.second] != INF) continue;

      lli sum;
      bool flag = false;
      if(!flag) {
        flag = true;
        sum = G[p.first][C];
        for(int j = 0; j < C; ++j) {
          if(j == p.second) continue;
          if(G[p.first][j] == INF) {
            flag = false;
            break;
          }
          sum -= G[p.first][j];
        }
      }
      if(!flag) {
        flag = true;
        sum = G[R][p.second];
        for(int i = 0; i < R; ++i) {
          if(i == p.first) continue;
          if(G[i][p.second] == INF) {
            flag = false;
            break;
          }
          sum -= G[i][p.second];
        }
      }

      if(flag) {
        G[p.first][p.second] = sum;
        if(--numR[p.first] == 1) {
          for(int j = 0; j < C; ++j) {
            if(G[p.first][j] == INF) {
              que.push(make_pair(p.first, j));
              break;
            }
          }
        }
        if(--numC[p.second] == 1) {
          for(int i = 0; i < R; ++i) {
            if(G[i][p.second] == INF) {
              que.push(make_pair(i, p.second));
              break;
            }
          }
        }
      }
    }

    vector<lli> ans;
    for(int i = 0; i < v.size(); ++i) {
      if(G[v[i].first][v[i].second] == INF) {
        ans.clear();
        break;
      }
      ans.push_back(G[v[i].first][v[i].second]);
    }

    if(first) {
      first = false;
    } else {
      cout << endl;
    }
    if(ans.size()) {
      for(int i = 0; i < ans.size(); ++i) {
        cout << ans[i] << endl;
      }
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1252
final_rank: 9

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt (string s) {int v; istringstream sin(s); sin>>v; return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iterator i=(c).begin();i!=(c).end();++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS=1e-10;
const double PI=acos(-1.0);


const int UNDEF=INT_MAX;
int main(){
	int p,s;
	int cc=0;
	while(cin>>p,p){
		if(cc){
			cout<<endl<<endl;
		}
		cin>>s;
		vvi cells(p+1,vi(s+1,UNDEF));
		vector<pii> undefs;
		REP(i,p+1){
			REP(j,s+1){
				string s;
				cin>>s;
				if(s=="?"){
					cells[i][j]=UNDEF;
					undefs.push_back(make_pair(i,j));
				}else{
					cells[i][j]=toInt(s);
				}
			}
		}

		vi ans(undefs.size());

		bool flag=true;
		vi used(undefs.size(),0);
		while(flag){
			flag=false;
			REP(kk,undefs.size()){
				if(used[kk])continue;
				pii u=undefs[kk];

				{
				int sum2=0;
				REP(i,s){
					if(cells[u.first][i]==UNDEF){
						if(i!=u.second){
							goto ng_s;
						}
					}else{
						sum2+=cells[u.first][i];
					}
				}
				ans[kk]=(cells[u.first][s]-sum2);
				cells[u.first][u.second]=ans[kk];
				used[kk]=1;
				flag=true;
				goto ng_p;
				}
ng_s:;
	 {
				int sum1=0;
				REP(i,p){
					if(cells[i][u.second]==UNDEF){
						if(i!=u.first){
							goto ng_p;
						}
					}else{
						sum1+=cells[i][u.second];
					}
				}
				ans[kk]=(cells[p][u.second]-sum1);
				cells[u.first][u.second]=ans[kk];
				used[kk]=1;
				flag=true;
	 }
	ng_p:;
			}
		}

		if(find(ALL(used),0)==used.end()){
			REP(i,ans.size()){
				if(i==ans.size()-1){
					cout<<ans[i];
				}else{
					cout<<ans[i]<<endl;
				}
			}
		}else{
			cout<<"NO";
		}
		cc++;
	}
	cout<<endl;

}
accuracy: 1
time_ms: 20
memory_kb: 0
final_rank: 5

code_snippet: #include<iostream>
#include<sstream>
using namespace std;

bool q[110][110],v[110][110],f;
int num[110][110],sum,pos;
int p,s,tp[110],ts[110],cq[110],rq[110];
string tmp;

int stoi(string x){
  stringstream ss(x);
  int n;
  ss >> n;
  return n;
}


int main(){
  f = true;
  while(cin >> p,p){
    if(!f)cout << endl;
    f = false;
    cin >> s; cin.ignore();
    
    for(int i=0;i<p;i++)rq[i] = 0;
    for(int i=0;i<s;i++)cq[i] = 0;

    for(int i=0;i<p;i++){
      for(int j=0;j<s;j++){
	cin >> tmp;
	if(tmp == "?"){
	  q[i][j] = v[i][j] = false;
	  rq[i]++; cq[j]++;
	}else{
	  q[i][j] = v[i][j] = true;
	  num[i][j] = stoi(tmp);
	}
      }
      cin >> tp[i]; cin.ignore();
    }
    for(int i=0;i<s;i++)cin >> ts[i];
    cin >> tmp;

    bool update = true;
    while(update){
      update = false;
      for(int i=0;i<p;i++){
	if(rq[i] == 1){
	  sum = 0;
	  for(int j=0;j<s;j++){
	    if(!v[i][j])pos = j;
	    else sum += num[i][j];
	  }
	  num[i][pos] = tp[i] - sum;
	  v[i][pos] = true;
	  rq[i]--; cq[pos]--;
	  update = true;
	}
      }
      for(int i=0;i<s;i++){
	if(cq[i] == 1){
	  sum = 0;
	  for(int j=0;j<p;j++){
	    if(!v[j][i])pos = j;
	    else sum += num[j][i];
	  }
	  num[pos][i] = ts[i] - sum;
	  v[pos][i] = true;
	  rq[pos]--; cq[i]--;
	  update = true;
	}
      }
    }

    int ans = 0;
    for(int i=0;i<p;i++)ans |= rq[i];
    for(int i=0;i<s;i++)ans |= cq[i];
    

    if(ans)cout << "NO\n";
    else{
      for(int i=0;i<p;i++){
	for(int j=0;j<s;j++){
	  if(!q[i][j])cout << num[i][j] << endl;
	}
      }
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

#define INF 1 << 28

string to_s(int n) {
    ostringstream oss;
    oss << n;
    return oss.str();
}

int to_i(string s) {
    int n;
    istringstream is;
    is.str(s);
    is >> n;
    return n;
}

int main() {
    int h, w;
    bool nikaime = false;
    while (cin >> h >> w) {
        int field[h+1][w+1];
        string s;
        vector<pair<int, int> > unknown; 
        for (int i = 0; i < h+1; i++) {
            for (int j = 0; j < w+1; j++) {
                cin >> s;
                if (s == "?") {
                    field[i][j] = INF;
                    unknown.push_back(pair<int,int>(i, j));
                } else {
                    field[i][j] = to_i(s);
                }
            }
        }

        for (int k = 0; k < 100; k++) {
        int unknown_x;
        for (int i = 0; i < h; i++) {
            int unknown_count = 0;
            for (int j = 0; j < w; j++) {
                if (field[i][j] == INF) {
                    unknown_count++;
                    unknown_x = j;
                }
            }
            if (unknown_count == 1) {
                int sum = 0;
                for (int j = 0; j < w; j++) {
                    if (j != unknown_x) {
                        sum += field[i][j];
                    }
                }
                field[i][unknown_x] = field[i][w] - sum;
            }
        }
                    
        int unknown_y;
        for (int i = 0; i < w; i++) {
            int unknown_count = 0;
            for (int j = 0; j < h; j++) {
                if (field[j][i] == INF) {
                    unknown_count++;
                    unknown_y = j;
                }
            }
            if (unknown_count == 1) {
                int sum = 0;
                for (int j = 0; j < h; j++) {
                    if (j != unknown_y) {
                        sum += field[j][i];
                    }
                }
                field[unknown_y][i] = field[h][i] - sum;
            }
        }
        }

//        int unknown_x;
//        for (int i = 0; i < h; i++) {
//            int unknown_count = 0;
//            for (int j = 0; j < w; j++) {
//                if (field[i][j] == INF) {
//                    unknown_count++;
//                    unknown_x = j;
//                }
//            }
//            if (unknown_count == 1) {
//                int sum = 0;
//                for (int j = 0; j < w; j++) {
//                    if (j != unknown_x) {
//                        sum += field[i][j];
//                    }
//                }
//                field[i][unknown_x] = field[i][w] - sum;
//            }
//        }

        string ans;
        for (int i = 0; i < unknown.size(); i++) {
            if (field[unknown[i].first][unknown[i].second] == INF) {
                ans = "NO\n";
                break;
            } else {
                ans += to_s(field[unknown[i].first][unknown[i].second]);
                ans += "\n";
            }
        }

        if (nikaime) {
            cout << endl;
        }

        cout << ans;

        nikaime = true;
 
//        for (int i = 0; i < h+1; i++) {
//            for (int j = 0; j < w+1; j++) {
//                if (field[i][j] == INF) {
//                    cout << "x ";
//                } else {
//                    cout << field[i][j] << ' ';
//                }
//            }
//            cout << endl;
//        }
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 964
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;

int p,s;
const int QST=1000000000;
int table[201][21];
int origTable[201][21];

int main(){
  bool fst=true;
  while(cin>>p&&p){
    if(!fst)cout<<endl;
    fst=false;
    int cnt=0;
    cin>>s;
    string str;
    for(int i=0;i<=p;i++){
      for(int j=0;j<=s;j++){
	cin>>str;
	if(str[0]=='?'){
	  table[i][j]=QST;
	  origTable[i][j]=table[i][j];
	  cnt++;
	}
	else{
	  int num=atoi(str.c_str());
	  table[i][j]=num;
	  origTable[i][j]=table[i][j];
	}
      }
    }
    bool update=true,isFail=false;
    while(update){
      update=false;
      for(int i=0;i<=p;i++){
	int c=0;
	int last=-1;
	for(int j=0;j<=s;j++){
	  if(table[i][j]==QST){
	    c++;
	    last=j;
	  }
	}
	if(c==1){
	  int sum=0;
	  if(last==s){
	    for(int j=0;j<s;j++)sum+=table[i][j];
	    table[i][last]=sum;
	  }
	  else{
	    sum=table[i][s];
	    for(int j=0;j<s;j++)if(table[i][j]!=QST)sum-=table[i][j];
	    table[i][last]=sum;
	  }
	  cnt--;
	  update=true;
	  break;
	}
      }
      for(int j=0;j<=s;j++){
	int c=0;
	int last=-1;
	for(int i=0;i<=p;i++){
	  if(table[i][j]==QST){
	    c++;
	    last=i;
	  }
	}
	if(c==1){
	  int sum=0;
	  if(last==p){
	    for(int i=0;i<p;i++)sum+=table[i][j];
	    table[last][j]=sum;
	  }
	  else{
	    sum=table[p][j];
	    for(int i=0;i<p;i++)if(table[i][j]!=QST)sum-=table[i][j];
	    table[last][j]=sum;
	  }
	  cnt--;
	  update=true;
	  break;
	}
      }
      if(cnt>0&&!update){
	isFail=true;
	break;
      }
      // 矛盾が起きてるかどうかを調べる
      for(int i=0;i<=p;i++){
	int sum=0;
	bool ok=true;
	for(int j=0;j<=s;j++){
	  if(table[i][j]==QST){
	    ok=false;break;
	  }
	  else if(j!=s)sum+=table[i][j];
	}
	// fail
	if(!ok)continue;
	// 全部埋まってる
	if(sum!=table[i][s]){
	  update=false;
	  isFail=true;
	  break;
	}
      }
      for(int j=0;j<=s;j++){
	int sum=0;
	bool ok=true;
	for(int i=0;i<=p;i++){
	  if(table[i][j]==QST){
	    ok=false;break;
	  }
	  else if(i!=p)sum+=table[i][j];
	}
	// fail
	if(!ok)continue;
	if(!(sum==table[p][j])){
	  update=false;
	  isFail=true;
	  break;
	}
      }
    }
    if(isFail)cout<<"NO"<<endl;
    else{
      for(int i=0;i<=p;i++)
	for(int j=0;j<=s;j++)
	  if(origTable[i][j]==QST)
	    cout<<table[i][j]<<endl;
    }
  }

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

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
typedef pair<int,int> P;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main(){
  int R,C;
  bool f = false;
  while(cin>>R>>C){
    if(f) cout<<endl;
    else f = true;
    int data[101][101];
    int cntx[101]={};
    int cnty[101]={};
    vector<P> fout;
    REP(y,R+1)REP(x,C+1){
      string s; cin>>s;
      if(s == "?") {
        data[y][x] = -INF;
        fout.push_back(P(x,y));
        cntx[x]++;
        cnty[y]++;
      } else {
        data[y][x] = atoi(s.c_str());
      }
    }
    while(true){
      bool updata = false;
      REP(y,R+1){
        if(cnty[y] == 1){
          updata = true;
          int sum = 0;
          REP(x,C+1)if(data[y][x]!=-INF)sum += ((x==C)?1:-1) * data[y][x];
          REP(x,C+1)if(data[y][x]==-INF){
            data[y][x] = ((x==C)?-1:1) * sum;
            cntx[x]--;
            cnty[y]--;
          }
        }
      }
      REP(x,C+1){
        if(cntx[x] == 1){
          cntx[x] = 0;
          updata = true;
          int sum = 0;
          REP(y,R+1)if(data[y][x]!=-INF)sum += ((y==R)?1:-1) * data[y][x];
          REP(y,R+1)if(data[y][x]==-INF){
            data[y][x] = ((y==R)?-1:1) * sum;
            cntx[x]--;
            cnty[y]--;
          }
        }
      }
      if(!updata) break;
    }
    int cnt = 0;
    REP(y,R+1)REP(x,C+1)if(data[y][x]==-INF) cnt++;
    if(cnt > 0) cout<<"NO"<<endl;
    else {
      REP(i, fout.size()){
        cout<<data[fout[i].second][fout[i].first]<<endl;
      }
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <utility>
#include <cstdlib>

using namespace std;
typedef pair<int, int> pii;
const int MISSING = 0x7fffffff;

int main(){
	bool first = true;
	while(true){
		int p, s;
		cin >> p;
		if(p == 0){ break; }
		if(!first){ cout << endl; }
		first = false;
		cin >> s;
		vector< vector<int> > table(p + 1, vector<int>(s + 1));
		vector< vector<bool> > missing(p, vector<bool>(s));
		for(int i = 0; i <= p; ++i){
			for(int j = 0; j <= s; ++j){
				string item;
				cin >> item;
				if(item == "?"){
					table[i][j] = MISSING;
					missing[i][j] = true;
				}else{
					table[i][j] = atoi(item.c_str());
				}
			}
		}
		queue<pii> q;
		for(int i = 0; i < p; ++i){
			int count = 0, pos = 0, sum = 0;
			for(int j = 0; j < s; ++j){
				if(table[i][j] == MISSING){
					++count;
					pos = j;
				}else{
					sum += table[i][j];
				}
			}
			if(count == 1){ table[i][pos] = table[i][s] - sum; }
		}
		for(int i = 0; i < s; ++i){
			int count = 0, pos = 0, sum = 0;
			for(int j = 0; j < p; ++j){
				if(table[j][i] == MISSING){
					++count;
					pos = j;
				}else{
					sum += table[j][i];
				}
			}
			if(count == 1){
				table[pos][i] = table[p][i] - sum;
				q.push(pii(0, pos));
			}
		}
		while(!q.empty()){
			pii e = q.front();
			q.pop();
			if(e.first == 0){
				int count = 0, pos = 0, sum = 0;
				for(int i = 0; i < s; ++i){
					if(table[e.second][i] == MISSING){
						++count;
						pos = i;
					}else{
						sum += table[e.second][i];
					}
				}
				if(count == 1){
					table[e.second][pos] = table[e.second][s] - sum;
					q.push(pii(1, pos));
				}
			}else{
				int count = 0, pos = 0, sum = 0;
				for(int i = 0; i < p; ++i){
					if(table[i][e.second] == MISSING){
						++count;
						pos = i;
					}else{
						sum += table[i][e.second];
					}
				}
				if(count == 1){
					table[pos][e.second] = table[p][e.second] - sum;
					q.push(pii(0, pos));
				}
			}
		}
		bool valid = true;
		vector<int> answer;
		for(int i = 0; i < p; ++i){
			for(int j = 0; j < s; ++j){
				if(table[i][j] == MISSING){
					valid = false;
				}else if(missing[i][j]){
					answer.push_back(table[i][j]);
				}
			}
		}
		if(!valid){
			cout << "NO" << endl;
		}else{
			for(int i = 0; i < answer.size(); ++i){
				cout << answer[i] << endl;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1