// atcoder/arc105/C/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/

#include <bits/stdc++.h>

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; }
template<typename T> istream& operator >> (istream& is, vector<T>& v) { for (auto& i: v) is >> i; return is; }
template<typename T> ostream& operator << (ostream& os, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; }

template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); }
template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); }

using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template<typename T> using vec = vector<T>;

constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 1e9 + 7;

int main(int argc, char *argv[])
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios_base::fixed);
  cout.precision(15);

  int n, m;
  while (cin >> n >> m) {
    vec<lli> w(n);
    cin >> w;
    vec<pair<lli, lli>> v(m);
    cin >> v;

    {
      int mx = *max_element(w.begin(), w.end());
      bool f = true;
      each (i, v) f = f && (mx <= i.second);
      unless (f) {
        cout << -1 << endl;
        continue;
      }
    }

    const int N = 10;
    const lli inf = 1LL << 60;
    lli mn = inf;

    sort(v.begin(), v.end(), [] (auto a, auto b) {
      if (a.second != b.second) return a.second < b.second;
      return a.first > b.first;
    });
    const int M = 1e5 + 5;
    static lli mx[M];
    fill(mx, mx + M, inf);
    vec<lli> u;
    for (int i = 0; i < m; ++i) {
      u.push_back(v[i].second);
      mx[i] = v[i].first;
      if (i) {
        setmax(mx[i], mx[i - 1]);
      }
    }

    sort(w.begin(), w.end());
    do {
      static lli g[N][N];
      fill(&g[0][0], &g[N - 1][N - 1] + 1, 0);
      for (int i = 0; i < n; ++i) {
        int sum = 0;
        for (int j = i; j < n; ++j) {
          sum += w[j];
          auto itr = lower_bound(u.begin(), u.end(), sum);
          if (itr != u.begin()) {
            --itr;
            setmax<lli>(g[i][j], mx[itr - u.begin()]);
          }
        }
      }

      // if (w == vec<lli>({1, 2, 4})) {
      //   for (int i = 0; i < n; ++i) {
      //     for (int j = 0; j < n; ++j) {
      //       cout << g[i][j] << ' ';
      //     }
      //     cout << endl;
      //   }
      // }

      static lli dp[N];
      fill(dp, dp + N, -inf);
      dp[0] = 0;
      for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
          setmax(dp[j], dp[i] + g[i][j]);
        }
      }
      setmin(mn, dp[n - 1]);
    } while (next_permutation(w.begin(), w.end()));
    cout << mn << endl;
  }

  return 0;
}

#include<iostream>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define int long long
using namespace std;
int c[502][502],a[502],b[502];
signed main(){
    ios::sync_with_stdio(false);
    int n; cin>>n;
    rep(i,1,n)rep(j,1,n)cin>>c[i][j];
    bool flag=true;
    rep(i,1,n){rep(j,2,n){
        if(i==1)a[j]=c[i][j]-c[i][j-1];
        else if(a[j]!=c[i][j]-c[i][j-1]){flag=false;break;}
    }if(!flag)break;}
    if(!flag){cout<<"No\n";return 0;}
    rep(j,1,n){rep(i,2,n){
        if(j==1)b[i]=c[i][j]-c[i-1][j];
        else if(b[i]!=c[i][j]-c[i-1][j]){flag=false;break;}
    }if(!flag)break;}
    if(!flag){cout<<"No\n";return 0;}
    int mna=0,mnb=0;
    rep(i,1,n){
        a[i]+=a[i-1],b[i]+=b[i-1];
        mna=min(mna,a[i]),mnb=min(mnb,b[i]);
    }
    //cout<<mna<<" "<<mnb<<endl;
    if(c[1][1]+mna+mnb<0){cout<<"No\n";return 0;}
    int ada=-mna,adb=c[1][1]-ada;
    cout<<"Yes\n";
    rep(i,1,n)cout<<b[i]+adb<<" ";
    cout<<endl;
    rep(i,1,n)cout<<a[i]+ada<<" ";
    cout<<endl;
    return 0;
}