//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 110000
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //§ß§é§à§é¨f§³
const ll Inf=0x3f3f3f3f3f3f3f3fll;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
//**************************************************************************************
namespace NetFlow
{
const ll MAXN=100000,MAXM=1000000,inf=0x3f3f3f3f3f3f3f3fll;
struct Edge
{
ll v,c,f,nx;
Edge() {}
Edge(ll v,ll c,ll f,ll nx):v(v),c(c),f(f),nx(nx) {}
} E[MAXM];
ll G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;
void init(ll _n)
{
N=_n,sz=0; memset(G,-1,sizeof(G[0])*N);
}
void link(ll u,ll v,ll c)
{
E[sz]=Edge(v,c,0,G[u]); G[u]=sz++;
E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;
}
bool bfs(int S,int T)
{
static int Q[MAXN]; memset(dis,-1,sizeof(dis[0])*N);
dis[S]=0; Q[0]=S;
for (int h=0,t=1,u,v,it;h<t;++h)
{
for (u=Q[h],it=G[u];~it;it=E[it].nx)
{
if (dis[v=E[it].v]==-1&&E[it].c>E[it].f)
{
dis[v]=dis[u]+1; Q[t++]=v;
}
}
}
return dis[T]!=-1;
}
ll dfs(ll u,ll T,ll low)
{
if (u==T) return low;
ll ret=0,tmp,v;
for (ll &it=cur[u];~it&&ret<low;it=E[it].nx)
{
if (dis[v=E[it].v]==dis[u]+1&&E[it].c>E[it].f)
{
if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
{
ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp;
}
}
}
if (!ret) dis[u]=-1; return ret;
}
ll dinic(int S,int T)
{
ll maxflow=0,tmp;
while (bfs(S,T))
{
memcpy(cur,G,sizeof(G[0])*N);
while (tmp=dfs(S,T,inf)) maxflow+=tmp;
}
return maxflow;
}
}
using namespace NetFlow;
int n,m;
struct node
{
ll x,y;
};
vector<node> EE[maxn];
ll d[maxn];
ll inq[maxn];
ll cost[maxn];
struct EEdge
{
ll x,y,z;
};
EEdge edge[maxn];
vector<ll> U,V;
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
{
edge[i].x=read(),edge[i].y=read(),edge[i].z=read();
EE[edge[i].x].push_back((node){edge[i].y,edge[i].z});
EE[edge[i].y].push_back((node){edge[i].x,edge[i].z});
}
for(int i=0;i<=n;i++)
d[i]=Inf;
queue<int> Q;
Q.push(1);
d[1]=0;
inq[1]=1;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
inq[u]=0;
for(int i=0;i<EE[u].size();i++)
{
node v = EE[u][i];
if(v.y+d[u]<d[v.x])
{
d[v.x]=v.y+d[u];
if(!inq[v.x])
{
Q.push(v.x);
inq[v.x]=1;
}
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<EE[i].size();j++)
{
node v = EE[i][j];
if(d[i]+v.y==d[v.x])
U.push_back(i+n),V.push_back(v.x);
}
}
init(100000);
for(int i=1;i<=n;i++)
{
cost[i]=read();
if(i!=1&&i!=n)
link(i,i+n,cost[i]);
else
link(i,i+n,inf);
}
for(int i=0;i<V.size();i++)
link(U[i],V[i],inf);
printf("%lld\n",dinic(1,2*n));
}