#include using namespace std; typedef long long ll; bool edge[5001][5001]; ll cost[5001][5001]; #define f(i) for (int i=n; i--;) #define f2(i,j) f(i) f(j) #define f3(i,j,k) f(i) f(j) f(k) int main(){ int n; cin>>n; ++n; ll res=0; for (int i=1; i>x>>t; res+=t; edge[i][i]=edge[x][i]=true; for (int j=0; j>cost[j][i]; cost[j][i]-=t; } } f3(i,j,k) edge[j][k]|=edge[j][i]&edge[i][k]; f2(i,j) if (edge[i][j]) f(k) cost[i][k]=min(cost[i][k],cost[j][k]); f2(i,j) if (edge[i][j]) cost[i][j]=0; for (int i=1; i