#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;
class SkiResorts { public:
long long minCost(vector <string> road, vector <int> altitude)
{
typedef int Vert;
typedef int Alt;
typedef pair<Vert,Alt> State;
typedef LL Cost;
typedef pair<Cost,State> Edge;
const int N = altitude.size();
priority_queue< Edge, vector<Edge>, greater<Edge> > Q;
set<State> V;
for(int i=0; i<N; ++i) {
State init(0, altitude[i]);
Q.push( Edge(abs(altitude[i]-altitude[0]), init) );
}
while( !Q.empty() ) {
Cost c = Q.top().first;
State s = Q.top().second;
Vert v = s.first;
Alt vh = s.second;
Q.pop();
if( V.count(s) )
continue;
V.insert(s);
if( v == N-1 )
return c;
for(Vert u=0; u<N; ++u) if(road[v][u] =='Y') {
Alt uh = altitude[u];
for(int i=0; i<N; ++i) {
Alt ch = altitude[i];
if( ch <= vh ) {
Cost cc = c + abs(ch-uh);
State ss(u, ch);
if(!V.count(ss))
Q.push(Edge(cc,ss));
}
}
}
}
return -1;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, SkiResorts().minCost(road, altitude));}
int main(){
CASE(0)
string road_[] = {"NYN",
"YNY",
"NYN"};
vector <string> road(road_, road_+sizeof(road_)/sizeof(*road_));
int altitude_[] = {30, 20, 10};
vector <int> altitude(altitude_, altitude_+sizeof(altitude_)/sizeof(*altitude_));
long long _ = 0LL;
END
CASE(1)
string road_[] = {"NY",
"YN"};
vector <string> road(road_, road_+sizeof(road_)/sizeof(*road_));
int altitude_[] = {10, 20};
vector <int> altitude(altitude_, altitude_+sizeof(altitude_)/sizeof(*altitude_));
long long _ = 10LL;
END
CASE(2)
string road_[] = {"NYN",
"YNN",
"NNN"};
vector <string> road(road_, road_+sizeof(road_)/sizeof(*road_));
int altitude_[] = {573, 573, 573};
vector <int> altitude(altitude_, altitude_+sizeof(altitude_)/sizeof(*altitude_));
long long _ = -1LL;
END
CASE(3)
string road_[] = {"NNYNNYYYNN",
"NNNNYNYNNN",
"YNNNNYYNNN",
"NNNNNNYNYY",
"NYNNNNNNYY",
"YNYNNNNYNN",
"YYYYNNNYNN",
"YNNNNYYNNN",
"NNNYYNNNNN",
"NNNYYNNNNN"};
vector <string> road(road_, road_+sizeof(road_)/sizeof(*road_));
int altitude_[] = {7, 4, 13, 2, 8, 1, 8, 15, 5, 15};
vector <int> altitude(altitude_, altitude_+sizeof(altitude_)/sizeof(*altitude_));
long long _ = 12LL;
END
CASE(4)
string road_[] = {
"NYNNNNNN",
"YNYNNNNN",
"NYNYNNNN",
"NNYNYNNN",
"NNNYNYNN",
"NNNNYNYN",
"NNNNNYNY",
"NNNNNNYN"
};
vector <string> road(road_, road_+sizeof(road_)/sizeof(*road_));
int altitude_[] = {
1,2,3,3,3,3,3,3
};
vector <int> altitude(altitude_, altitude_+sizeof(altitude_)/sizeof(*altitude_));
long long _ = 3LL;
END
/*
CASE(5)
string road_[] = ;
vector <string> road(road_, road_+sizeof(road_)/sizeof(*road_));
int altitude_[] = ;
vector <int> altitude(altitude_, altitude_+sizeof(altitude_)/sizeof(*altitude_));
long long _ = LL;
END
*/
}
// END CUT HERE