import java.util.Scanner;

public class P25C {
    public P25C() {
        Scanner sc = new Scanner (System.in);
        int n = sc.nextInt();
        long sum = 0;
        int [][]distances = new int [n+1][n+1];
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n;j++){
                distances[i][j] = sc.nextInt();
                if (j < i){
                    sum += distances[i][j];
                }
            }
        }

        int t = sc.nextInt();
        for (int l = 0; l < t; l++){
            int x = sc.nextInt();
            int y = sc.nextInt();
            int z = sc.nextInt();
            if (distances[x][y] > z){
                distances[x][y] = z;
                distances[y][x] = z;
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j <= n; j++){
                        if (distances[i][j] > distances[i][x] + distances[y][j] + distances[x][y]){
                            distances[i][j] = distances[i][x] + distances[y][j] + distances[x][y];
                            distances[j][i] = distances[i][x] + distances[y][j] + distances[x][y];
                        }
                    }
                }
//              for (int k = 1; k <= n; k++){
//                  for (int i = 1; i <= n; i++){
//                      for (int j = 1; j <= n; j++){
//                          if (distances[i][j] > distances[i][k] + distances[k][j]){
//                              distances[i][j] = distances[i][k] + distances[k][j];
//                          }
//                      }
//                  }
//
//              }
                sum = 0;
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j < i; j++){
                        sum += distances[i][j];
                    }
                }
            }
            System.out.print(sum + " ");
        }

        sc.close();
    }

    public static void main (String []args){
        new P25C();
    }
}


import java.util.Scanner;

public class P25C {
    public P25C() {
        Scanner sc = new Scanner (System.in);
        int n = sc.nextInt();
        int sum = 0;
        int [][]distances = new int [n+1][n+1];
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n;j++){
                distances[i][j] = sc.nextInt();
                if (j < i){
                    sum += distances[i][j];
                }
            }
        }

        int t = sc.nextInt();
        for (int l = 0; l < t; l++){
            int x = sc.nextInt();
            int y = sc.nextInt();
            int z = sc.nextInt();
            if (distances[x][y] > z){
                distances[x][y] = z;
                distances[y][x] = z;
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j <= n; j++){
                        if (distances[i][j] > distances[i][x] + distances[y][j] + distances[x][y]){
                            distances[i][j] = distances[i][x] + distances[y][j] + distances[x][y];
                        }
                    }
                }

                sum = 0;
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j < i; j++){
                        sum += distances[i][j];
                    }
                }
            }
            System.out.print(sum + " ");
        }

        sc.close();
    }

    public static void main (String []args){
        new P25C();
    }
}


import java.util.Scanner;

public class P25C {
    public P25C() {
        Scanner sc = new Scanner (System.in);
        int n = sc.nextInt();
        int sum = 0;
        int [][]distances = new int [n+1][n+1];
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n;j++){
                distances[i][j] = sc.nextInt();
                if (j < i){
                    sum += distances[i][j];
                }
            }
        }

        int t = sc.nextInt();
        for (int l = 0; l < t; l++){
            int x = sc.nextInt();
            int y = sc.nextInt();
            int z = sc.nextInt();
            if (distances[x][y] > z){
                distances[x][y] = z;
                distances[y][x] = z;
                for (int k = 1; k <= n; k++){
                    for (int i = 1; i <= n; i++){
                        for (int j = 1; j <= n; j++){
                            if (distances[i][j] > distances[i][k] + distances[k][j]){
                                distances[i][j] = distances[i][k] + distances[k][j];
                            }
                        }
                    }
                }

                sum = 0;
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j < i; j++){
                        sum += distances[i][j];
                    }
                }
            }
            System.out.print(sum + " ");
        }

        sc.close();
    }

    public static void main (String []args){
        new P25C();
    }
}


import java.util.Scanner;

public class P25C {
    public P25C() {
        Scanner sc = new Scanner (System.in);
        int n = sc.nextInt();
        int sum = 0;
        int [][]distances = new int [n+1][n+1];
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= n;j++){
                distances[i][j] = sc.nextInt();
                if (j < i){
                    sum += distances[i][j];
                }
            }
        }

        int t = sc.nextInt();
        for (int l = 0; l < t; l++){
            int x = sc.nextInt();
            int y = sc.nextInt();
            int z = sc.nextInt();
            if (distances[x][y] > z){
                distances[x][y] = z;
                distances[y][x] = z;
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j <= n; j++){
                        if (distances[i][j] > distances[i][x] + distances[y][j] + distances[x][y]){
                            distances[i][j] = distances[i][x] + distances[y][j] + distances[x][y];
                            distances[j][i] = distances[i][x] + distances[y][j] + distances[x][y];
                        }
                    }
                }
//              for (int k = 1; k <= n; k++){
//                  for (int i = 1; i <= n; i++){
//                      for (int j = 1; j <= n; j++){
//                          if (distances[i][j] > distances[i][k] + distances[k][j]){
//                              distances[i][j] = distances[i][k] + distances[k][j];
//                          }
//                      }
//                  }
//
//              }
                sum = 0;
                for (int i = 1; i <= n; i++){
                    for (int j = 1; j < i; j++){
                        sum += distances[i][j];
                    }
                }
            }
            System.out.print(sum + " ");
        }

        sc.close();
    }

    public static void main (String []args){
        new P25C();
    }
}


import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Author   : Dylan
 * Date     : 2013-08-10
 * Time     : 12:46
 * Project  : Roads in Berland
 */

public class Main {
    static int[][] graph;
    static int n, k;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        graph = new int[n + 1][n + 1];
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                graph[i][j] = in.nextInt();
                if(j > i) ans += graph[i][j];
            }
        }
        k = in.nextInt();
        for(int i = 0; i < k; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            int l = in.nextInt();
            if(graph[x][y] > l) {
                for(int j = 1; j <= n; j++) {
                    if(l + graph[y][j] < graph[x][j]) {
                        ans -= graph[x][j] - l - graph[y][j];
                        graph[x][j] = l + graph[y][j];
                        graph[j][x] = graph[x][j];
                    }
                }
                for(int j = 1;j <= n; j++) {
                    if(graph[j][x] + l < graph[j][y]) {
                        ans -= graph[j][y] - l - graph[j][x];
                        graph[j][y] = graph[j][x] + l;
                        graph[y][j] = graph[j][y];
                    }
                }
            }
            System.out.print(ans + " ");
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Author   : Dylan
 * Date     : 2013-08-10
 * Time     : 12:46
 * Project  : Roads in Berland
 */

public class Main {
    static int[][] graph;
    static int n, k;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        graph = new int[n + 1][n + 1];
        long ans = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                graph[i][j] = in.nextInt();
                if(j > i) ans += graph[i][j];
            }
        }
        k = in.nextInt();
        while(k-- > 0) {
            int x = in.nextInt();
            int y = in.nextInt();
            int l = in.nextInt();
            if(graph[x][y] > l) {
                for(int i = 1; i <= n; i++) {
                    for(int j = 1; j <= n; j++) {
                        if(graph[i][x] + l + graph[y][j] < graph[i][j]) {
                            ans -= graph[i][j] - (graph[i][x] + l + graph[y][j]);
                            graph[i][j] = graph[i][x] + l + graph[y][j];
                            graph[j][i] = graph[i][j];
                        }
                    }
                }
                /*
                for(int i = 1; i <= n; i++) {
                    if(l + graph[y][i] < graph[x][i]) {
                        ans -= graph[x][i] - l - graph[y][i];
                        graph[x][i] = l + graph[y][i];
                        graph[i][x] = graph[x][i];
                    }
                }
                for(int i = 1;i <= n; i++) {
                    if(graph[i][x] + l < graph[i][y]) {
                        ans -= graph[i][y] - l - graph[i][x];
                        graph[i][y] = graph[i][x] + l;
                        graph[y][i] = graph[i][y];
                    }
                }
                */
            }
            System.out.print(ans + " ");
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Author   : Dylan
 * Date     : 2013-08-10
 * Time     : 12:46
 * Project  : Roads in Berland
 */

public class Main {
    static int[][] graph;
    static int n, k;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        graph = new int[n + 1][n + 1];
        long ans = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                graph[i][j] = in.nextInt();
                if(j > i) ans += graph[i][j];
            }
        }
        k = in.nextInt();
        while(k-- > 0) {
            int x = in.nextInt();
            int y = in.nextInt();
            int l = in.nextInt();
            if(graph[x][y] > l) {
                for(int i = 1; i <= n; i++) {
                    for(int j = 1; j <= n; j++) {
                        if(graph[i][x] + l + graph[y][j] < graph[i][j]) {
                            ans -= graph[i][j] - (graph[i][x] + l + graph[y][j]);
                            graph[i][j] = graph[i][x] + l + graph[y][j];
                            graph[j][i] = graph[i][j];
                        }
                    }
                }
            }
            System.out.print(ans + " ");
        }
    }
}


import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Author   : Dylan
 * Date     : 2013-08-10
 * Time     : 12:46
 * Project  : Roads in Berland
 */

public class Main {
    static int[][] graph;
    static int n, k;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        graph = new int[n + 1][n + 1];
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                graph[i][j] = in.nextInt();
                if (j > i) ans += graph[i][j];
            }
        }
        k = in.nextInt();
        while (k-- > 0) {
            int x = in.nextInt();
            int y = in.nextInt();
            int l = in.nextInt();
            if (graph[x][y] > l) {
                boolean updata = true;
                while (updata) {
                    updata = false;
                    for (int i = 1; i <= n; i++) {
                        if (l + graph[y][i] < graph[x][i]) {
                            ans -= graph[x][i] - l - graph[y][i];
                            graph[x][i] = l + graph[y][i];
                            graph[i][x] = graph[x][i];
                            updata = true;
                        }
                    }
                    for (int i = 1; i <= n; i++) {
                        if (graph[i][x] + l < graph[i][y]) {
                            ans -= graph[i][y] - l - graph[i][x];
                            graph[i][y] = graph[i][x] + l;
                            graph[y][i] = graph[i][y];
                            updata = true;
                        }
                    }
                }
            }
            System.out.print(ans + " ");
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Author   : Dylan
 * Date     : 2013-08-10
 * Time     : 12:46
 * Project  : Roads in Berland
 */

public class Main {
    static int[][] graph;
    static int n, k;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        graph = new int[n + 1][n + 1];
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                graph[i][j] = in.nextInt();
                if(j > i) ans += graph[i][j];
            }
        }
        k = in.nextInt();
        while(k-- > 0) {
            int x = in.nextInt();
            int y = in.nextInt();
            int l = in.nextInt();
            if(graph[x][y] > l) {
                for(int i = 1; i <= n; i++) {
                    for(int j = 1; j <= n; j++) {
                        if(graph[i][x] + l + graph[y][j] < graph[i][j]) {
                            ans -= graph[i][j] - (graph[i][x] + l + graph[y][j]);
                            graph[i][j] = graph[i][x] + l + graph[y][j];
                            graph[j][i] = graph[i][j];
                        }
                    }
                }
                /*
                for(int i = 1; i <= n; i++) {
                    if(l + graph[y][i] < graph[x][i]) {
                        ans -= graph[x][i] - l - graph[y][i];
                        graph[x][i] = l + graph[y][i];
                        graph[i][x] = graph[x][i];
                    }
                }
                for(int i = 1;i <= n; i++) {
                    if(graph[i][x] + l < graph[i][y]) {
                        ans -= graph[i][y] - l - graph[i][x];
                        graph[i][y] = graph[i][x] + l;
                        graph[y][i] = graph[i][y];
                    }
                }
                */
            }
            System.out.print(ans + " ");
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Author   : Dylan
 * Date     : 2013-08-10
 * Time     : 12:46
 * Project  : Roads in Berland
 */

public class Main {
    static int[][] graph;
    static int n, k;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        graph = new int[n + 1][n + 1];
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                graph[i][j] = in.nextInt();
                if(j > i) ans += graph[i][j];
            }
        }
        k = in.nextInt();
        while(k-- > 0) {
            int x = in.nextInt();
            int y = in.nextInt();
            int l = in.nextInt();
            if(graph[x][y] > l) {
                for(int i = 1; i <= n; i++) {
                    if(l + graph[y][i] < graph[x][i]) {
                        ans -= graph[x][i] - l - graph[y][i];
                        graph[x][i] = l + graph[y][i];
                        graph[i][x] = graph[x][i];
                    }
                }
                for(int i = 1;i <= n; i++) {
                    if(graph[i][x] + l < graph[i][y]) {
                        ans -= graph[i][y] - l - graph[i][x];
                        graph[i][y] = graph[i][x] + l;
                        graph[y][i] = graph[i][y];
                    }
                }
            }
            ans = 0;
            for(int i = 1; i <= n; i++)
                for(int j = i + 1; j <= n; j++)
                    ans += graph[i][j];
            System.out.print(ans + " ");
        }
    }
}


import java.util.Scanner;

public class c25 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int INF = Integer.MAX_VALUE / 2;
        int n = in.nextInt();
        int[][] adjMat = new int[n + 1][n + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                adjMat[i][j] = in.nextInt();
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == j) {

                } else if (adjMat[i][j] == 0)
                    adjMat[i][j] = INF;
            }
        }

        int k1 = in.nextInt();
        long sum = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (adjMat[i][j] < INF)
                    sum += adjMat[i][j];
            }
        }
        //System.out.println(sum);
        while (k1-- != 0) {
            int f = in.nextInt();
            int t = in.nextInt();
            int w = in.nextInt();

                sum = 0;
                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= n; j++) {
                        sum+=adjMat[i][j]=Math.min(adjMat[i][j], Math.min(adjMat[i][f]+w+adjMat[t][j], adjMat[i][t]+w+adjMat[f][j]));
                    }
                }
                System.out.print(sum/2);
                if(k1>0)
                    System.out.print(" ");

        }
    }

}


import java.util.*;
import java.io.*;

public class Main implements Runnable {

	public void solve() throws IOException {
		int N = nextInt();
                long sum = 0;
                int[][] grid = new int[N][N];
                for(int i = 0; i < N; i++)
                        for(int j = 0; j < N; j++)
                        {grid[i][j] = nextInt(); sum += grid[i][j]; }

                int M = nextInt();
                for(int k  = 0; k < M; k++){

                        int u = nextInt() - 1;
                        int v = nextInt() - 1;
                        int c = nextInt();
                        if(grid[u][v] <= c){
                                System.out.println(sum / 2);
                                continue;
                        }

                        sum = 0;
                        grid[u][v] = c; grid[v][u] = c;

                        for(int i = 0; i < N; i++){
                                for(int j = 0; j < N; j++){
                                        grid[i][j] = Math.min(grid[i][j], grid[i][u] + grid[u][v] + grid[v][j]);
                                        grid[i][j] = Math.min(grid[i][j], grid[i][v] + grid[v][u] + grid[u][j]);
                                }
                        }

                        for(int i = 0; i < N; i++){
                                for(int j = 0; j < N; j++){
                                        sum += grid[i][j];
                                }
                        }

                        System.out.println(sum/2);
                }

	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class R25qCRoadsInBerland {

	public static void main(String args[] ) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    PrintWriter w = new PrintWriter(System.out);

	    StringTokenizer st1 = new StringTokenizer(br.readLine());
	    int n = ip(st1.nextToken());

	    long dist[][] = new long[n][n];
	    for(int i=0;i<n;i++){
	    	StringTokenizer st = new StringTokenizer(br.readLine());
	    	for(int j=0;j<n;j++)
	    		dist[i][j] = ip(st.nextToken());
	    }

	    int k = ip(br.readLine());

	    for(int i=0;i<k;i++){
	    	StringTokenizer st2 = new StringTokenizer(br.readLine());
	    	int a = ip(st2.nextToken()) - 1;
	    	int b = ip(st2.nextToken()) - 1;
	    	int c = ip(st2.nextToken());
	    	long sum = 0;
	    	for(int x=0;x<n;x++){
	    		for(int y=0;y<n;y++){
	    			dist[x][y] = Math.min(dist[x][y], dist[x][a] + c + dist[b][y]);
	    			dist[x][y] = Math.min(dist[x][y], dist[x][b] + c + dist[a][y]);
	    			sum += dist[x][y];
	    		}
	    	}
	    	w.print(sum/2);
	    	w.print(" ");
	    }

	    w.close();
	}

	public static int ip(String s){
		return Integer.parseInt(s);
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class R25qCRoadsInBerland {

	public static void main(String args[] ) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    PrintWriter w = new PrintWriter(System.out);

	    StringTokenizer st1 = new StringTokenizer(br.readLine());
	    int n = ip(st1.nextToken());

	    int dist[][] = new int[n][n];
	    for(int i=0;i<n;i++){
	    	StringTokenizer st = new StringTokenizer(br.readLine());
	    	for(int j=0;j<n;j++)
	    		dist[i][j] = ip(st.nextToken());
	    }

	    int k = ip(br.readLine());

	    for(int i=0;i<k;i++){
	    	StringTokenizer st2 = new StringTokenizer(br.readLine());
	    	int a = ip(st2.nextToken()) - 1;
	    	int b = ip(st2.nextToken()) - 1;
	    	int c = ip(st2.nextToken());
	    	int sum = 0;
	    	for(int x=0;x<n;x++){
	    		for(int y=0;y<n;y++){
	    			dist[x][y] = Math.min(dist[x][y], dist[x][a] + c + dist[b][y]);
	    			dist[x][y] = Math.min(dist[x][y], dist[x][b] + c + dist[a][y]);
	    			sum += dist[x][y];
	    		}
	    	}
	    	w.print(sum/2);
	    	w.print(" ");
	    }

	    w.close();
	}

	public static int ip(String s){
		return Integer.parseInt(s);
	}
}


import java.util.Scanner;

public class Roads_in_Berland {

    class Road {
        public int i, j, c;

        public Road(int i1, int j1, int c1) {
            i = i1;
            j = j1;
            c = c1;
        }

    }

    public int n, k;
    public long[][] dist;
    public Road[] newRoads;

    void read() {
        Scanner read = new Scanner(System.in);
        n = read.nextInt();
        dist = new long[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dist[i][j] = read.nextInt();
            }
        }

        k = read.nextInt();
        newRoads = new Road[k];
        for (int i = 0; i < k; i++) {
            int i1 = read.nextInt() - 1;
            int j1 = read.nextInt() - 1;
            int c1 = read.nextInt();
            newRoads[i] = new Road(i1, j1, c1);
        }

    }

    public int sum() {
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                res += dist[i][j];
            }
        }
        return res;
    }

    public void solve() {
        read();

        int[] results = new int[k];
        for (int r = 0; r < k; r++) {
            if (newRoads[r].c <= dist[newRoads[r].i][newRoads[r].j]) {
                dist[newRoads[r].i][newRoads[r].j] = dist[newRoads[r].j][newRoads[r].i] = newRoads[r].c;
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if (dist[i][newRoads[r].i] + newRoads[r].c
                                + dist[newRoads[r].j][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].i]
                                    + newRoads[r].c + dist[newRoads[r].j][j];
                        } else if (dist[i][newRoads[r].j] + newRoads[r].c
                                + dist[newRoads[r].i][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].j]
                                    + newRoads[r].c + dist[newRoads[r].i][j];

                        }
                    }
                }
            }

            int res = sum();
            results[r] = res;
        }

        for (int i = 0; i < k - 1; i++) {
            System.out.print(results[i] + " ");
        }
        System.out.println(results[k - 1]);
    }

    public static void main(String[] args) {
        new Roads_in_Berland().solve();
    }

}


import java.util.Scanner;

public class Roads_in_Berland {

    class Road {
        public int i, j, c;

        public Road(int i1, int j1, int c1) {
            i = i1;
            j = j1;
            c = c1;
        }

    }

    public int n, k;
    public int[][] dist;
    public Road[] newRoads;

    void read() {
        Scanner read = new Scanner(System.in);
        n = read.nextInt();
        dist = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dist[i][j] = read.nextInt();
            }
        }

        k = read.nextInt();
        newRoads = new Road[k];
        for (int i = 0; i < k; i++) {
            int i1 = read.nextInt() - 1;
            int j1 = read.nextInt() - 1;
            int c1 = read.nextInt();
            newRoads[i] = new Road(i1, j1, c1);
        }

    }

    public int sum() {
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                res += dist[i][j];
            }
        }
        return res;
    }

    public void solve() {
        read();

        int [] results=new int[k];
        for (int r = 0; r < k; r++) {
            for (int i = 0; i < n ; i++) {
                for (int j = 0; j < n; j++) {
                    if (newRoads[r].c <= dist[newRoads[r].i][newRoads[r].j]) {
                        dist[newRoads[r].i][newRoads[r].j]=dist[newRoads[r].j][newRoads[r].i] = newRoads[r].c;
                        if (dist[i][newRoads[r].i] + newRoads[r].c
                                + dist[newRoads[r].j][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].i]
                                    + newRoads[r].c + dist[newRoads[r].j][j];
                        } else if (dist[i][newRoads[r].j] + newRoads[r].c
                                + dist[newRoads[r].i][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].j]
                                    + newRoads[r].c + dist[newRoads[r].i][j];

                        }
                    }
                }
            }

            int res=sum();
            results[r]=res;
        }

        for(int i=0;i<k-1;i++){
            System.out.print(results[i]+" ");
        }
        System.out.println(results[k-1]);
    }

    public static void main(String[] args) {
        new Roads_in_Berland().solve();
    }

}


import java.util.Scanner;

public class Roads_in_Berland {

    class Road {
        public int i, j, c;

        public Road(int i1, int j1, int c1) {
            i = i1;
            j = j1;
            c = c1;
        }

    }

    public int n, k;
    public int[][] dist;
    public Road[] newRoads;

    void read() {
        Scanner read = new Scanner(System.in);
        n = read.nextInt();
        dist = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dist[i][j] = read.nextInt();
            }
        }

        k = read.nextInt();
        newRoads = new Road[k];
        for (int i = 0; i < k; i++) {
            int i1 = read.nextInt() - 1;
            int j1 = read.nextInt() - 1;
            int c1 = read.nextInt();
            newRoads[i] = new Road(i1, j1, c1);
        }

    }

    public int sum() {
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                res += dist[i][j];
            }
        }
        return res;
    }

    public void solve() {
        read();

        int [] results=new int[k];
        for (int r = 0; r < k; r++) {
            for (int i = 0; i < n - 1; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (newRoads[r].c <= dist[newRoads[r].i][newRoads[r].j]) {
                        dist[newRoads[r].i][newRoads[r].j] = newRoads[r].c;
                        if (dist[i][newRoads[r].i] + newRoads[r].c
                                + dist[newRoads[r].j][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].i]
                                    + newRoads[r].c + dist[newRoads[r].j][j];
                        } else if (dist[i][newRoads[r].j] + newRoads[r].c
                                + dist[newRoads[r].i][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].j]
                                    + newRoads[r].c + dist[newRoads[r].i][j];

                        }
                    }
                }
            }

            int res=sum();
            results[r]=res;
        }

        for(int i=0;i<k-1;i++){
            System.out.print(results[i]+" ");
        }
        System.out.println(results[k-1]);
    }

    public static void main(String[] args) {
        new Roads_in_Berland().solve();
    }

}


import java.util.Scanner;

public class Roads_in_Berland {

    class Road {
        public int i, j, c;

        public Road(int i1, int j1, int c1) {
            i = i1;
            j = j1;
            c = c1;
        }

    }

    public int n, k;
    public int[][] dist;
    public Road[] newRoads;

    void read() {
        Scanner read = new Scanner(System.in);
        n = read.nextInt();
        dist = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dist[i][j] = read.nextInt();
            }
        }

        k = read.nextInt();
        newRoads = new Road[k];
        for (int i = 0; i < k; i++) {
            int i1 = read.nextInt() - 1;
            int j1 = read.nextInt() - 1;
            int c1 = read.nextInt();
            newRoads[i] = new Road(i1, j1, c1);
        }

    }

    public int sum() {
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                res += dist[i][j];
            }
        }
        return res;
    }

    public void solve() {
        read();

        int [] results=new int[k];
        for (int r = 0; r < k; r++) {
            for (int i = 0; i < n - 1; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (newRoads[r].c <= dist[newRoads[r].i][newRoads[r].j]) {
                        dist[newRoads[r].i][newRoads[r].j]=dist[newRoads[r].j][newRoads[r].i] = newRoads[r].c;
                        if (dist[i][newRoads[r].i] + newRoads[r].c
                                + dist[newRoads[r].j][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].i]
                                    + newRoads[r].c + dist[newRoads[r].j][j];
                        } else if (dist[i][newRoads[r].j] + newRoads[r].c
                                + dist[newRoads[r].i][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].j]
                                    + newRoads[r].c + dist[newRoads[r].i][j];

                        }
                    }
                }
            }

            int res=sum();
            results[r]=res;
        }

        for(int i=0;i<k-1;i++){
            System.out.print(results[i]+" ");
        }
        System.out.println(results[k-1]);
    }

    public static void main(String[] args) {
        new Roads_in_Berland().solve();
    }

}


import java.util.Scanner;

public class Roads_in_Berland {

    class Road {
        public int i, j, c;

        public Road(int i1, int j1, int c1) {
            i = i1;
            j = j1;
            c = c1;
        }

    }

    public int n, k;
    public int[][] dist;
    public Road[] newRoads;

    void read() {
        Scanner read = new Scanner(System.in);
        n = read.nextInt();
        dist = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dist[i][j] = read.nextInt();
            }
        }

        k = read.nextInt();
        newRoads = new Road[k];
        for (int i = 0; i < k; i++) {
            int i1 = read.nextInt() - 1;
            int j1 = read.nextInt() - 1;
            int c1 = read.nextInt();
            newRoads[i] = new Road(i1, j1, c1);
        }

    }

    public int sum() {
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                res += dist[i][j];
            }
        }
        return res;
    }

    public void solve() {
        read();

        int[] results = new int[k];
        for (int r = 0; r < k; r++) {
            if (newRoads[r].c <= dist[newRoads[r].i][newRoads[r].j]) {
                dist[newRoads[r].i][newRoads[r].j] = dist[newRoads[r].j][newRoads[r].i] = newRoads[r].c;
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if (dist[i][newRoads[r].i] + newRoads[r].c
                                + dist[newRoads[r].j][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].i]
                                    + newRoads[r].c + dist[newRoads[r].j][j];
                        } else if (dist[i][newRoads[r].j] + newRoads[r].c
                                + dist[newRoads[r].i][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].j]
                                    + newRoads[r].c + dist[newRoads[r].i][j];

                        }
                    }
                }
            }

            int res = sum();
            results[r] = res;
        }

        for (int i = 0; i < k - 1; i++) {
            System.out.print(results[i] + " ");
        }
        System.out.println(results[k - 1]);
    }

    public static void main(String[] args) {
        new Roads_in_Berland().solve();
    }

}


import java.util.Scanner;

public class Roads_in_Berland {

    class Road {
        public int i, j, c;

        public Road(int i1, int j1, int c1) {
            i = i1;
            j = j1;
            c = c1;
        }

    }

    public int n, k;
    public long[][] dist;
    public Road[] newRoads;

    void read() {
        Scanner read = new Scanner(System.in);
        n = read.nextInt();
        dist = new long[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dist[i][j] = read.nextInt();
            }
        }

        k = read.nextInt();
        newRoads = new Road[k];
        for (int i = 0; i < k; i++) {
            int i1 = read.nextInt() - 1;
            int j1 = read.nextInt() - 1;
            int c1 = read.nextInt();
            newRoads[i] = new Road(i1, j1, c1);
        }

    }

    public long sum() {
        long res = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                res += dist[i][j];
            }
        }
        return res;
    }

    public void solve() {
        read();

        long[] results = new long[k];
        for (int r = 0; r < k; r++) {
            if (newRoads[r].c <= dist[newRoads[r].i][newRoads[r].j]) {
                dist[newRoads[r].i][newRoads[r].j] = dist[newRoads[r].j][newRoads[r].i] = newRoads[r].c;
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        if (dist[i][newRoads[r].i] + newRoads[r].c
                                + dist[newRoads[r].j][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].i]
                                    + newRoads[r].c + dist[newRoads[r].j][j];
                        } else if (dist[i][newRoads[r].j] + newRoads[r].c
                                + dist[newRoads[r].i][j] < dist[i][j]) {
                            dist[i][j] = dist[j][i] = dist[i][newRoads[r].j]
                                    + newRoads[r].c + dist[newRoads[r].i][j];

                        }
                    }
                }
            }

            long res = sum();
            results[r] = res;
        }

        for (int i = 0; i < k - 1; i++) {
            System.out.print(results[i] + " ");
        }
        System.out.println(results[k - 1]);
    }

    public static void main(String[] args) {
        new Roads_in_Berland().solve();
    }

}


import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Zyflair Griffane
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		PandaScanner in = new PandaScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		C solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
}

class C {
    public void solve(int testNumber, PandaScanner in, PrintWriter out) {
        int n = in.nextInt();
        long[][] adj = new long[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                adj[i][j] = in.nextInt();
            }
        }
        for (int z = 0; z < n; z++) {
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    adj[x][y] = Math.min(adj[x][y], adj[x][z] + adj[z][y]);
                }
            }
        }
        int k = in.nextInt();
        long res[] = new long[k];
        for (int i = 0; i < k; i++) {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            int d = in.nextInt();
            adj[u][v] = adj[v][u] = Math.min(adj[u][v], d);
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    adj[a][b] = Math.min(adj[a][b], adj[a][u] + adj[u][b]);
                }
            }
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    adj[a][b] = Math.min(adj[a][b], adj[a][v] + adj[v][b]);
                }
            }
            for (int a = 0; a < n; a++) {
                for (int b = a; b < n; b++) {
                    res[i] += adj[a][b];
                }
            }
        }
        out.println(Arrays.toString(res).replaceAll("[^ 0-9]", ""));
    }

}

class PandaScanner {
    public BufferedReader br;
    public StringTokenizer st;
    public InputStream in;

    public PandaScanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(this.in = in));
    }

    public String nextLine() {
        try {
            return br.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String next() {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nextLine().trim());
            return next();
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }


import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Zyflair Griffane
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		PandaScanner in = new PandaScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		C solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
}

class C {
    public void solve(int testNumber, PandaScanner in, PrintWriter out) {
        int n = in.nextInt();
        long[][] adj = new long[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                adj[i][j] = in.nextInt();
            }
        }
        int k = in.nextInt();
        int res[] = new int[k];
        for (int i = 0; i < k; i++) {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            int d = in.nextInt();
            adj[u][v] = adj[v][u] = Math.min(adj[u][v], d);
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    adj[a][b] = Math.min(adj[a][b], adj[a][u] + adj[u][b]);
                }
            }
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    adj[a][b] = Math.min(adj[a][b], adj[a][v] + adj[v][b]);
                }
            }
            for (int a = 0; a < n; a++) {
                for (int b = a; b < n; b++) {
                    res[i] += adj[a][b];
                }
            }
        }
        out.println(Arrays.toString(res).replaceAll("[^ 0-9]", ""));
    }

}

class PandaScanner {
    public BufferedReader br;
    public StringTokenizer st;
    public InputStream in;

    public PandaScanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(this.in = in));
    }

    public String nextLine() {
        try {
            return br.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String next() {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nextLine().trim());
            return next();
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }


import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Zyflair Griffane
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		PandaScanner in = new PandaScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		C solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
}

class C {
    public void solve(int testNumber, PandaScanner in, PrintWriter out) {
        int n = in.nextInt();
        long[][] adj = new long[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                adj[i][j] = in.nextInt();
            }
        }
        for (int z = 0; z < n; z++) {
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    adj[x][y] = Math.min(adj[x][y], adj[x][z] + adj[z][y]);
                }
            }
        }
        int k = in.nextInt();
        int res[] = new int[k];
        for (int i = 0; i < k; i++) {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            int d = in.nextInt();
            adj[u][v] = adj[v][u] = Math.min(adj[u][v], d);
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    adj[a][b] = Math.min(adj[a][b], adj[a][u] + adj[u][b]);
                }
            }
            for (int a = 0; a < n; a++) {
                for (int b = 0; b < n; b++) {
                    adj[a][b] = Math.min(adj[a][b], adj[a][v] + adj[v][b]);
                }
            }
            for (int a = 0; a < n; a++) {
                for (int b = a; b < n; b++) {
                    res[i] += adj[a][b];
                }
            }
        }
        out.println(Arrays.toString(res).replaceAll("[^ 0-9]", ""));
    }

}

class PandaScanner {
    public BufferedReader br;
    public StringTokenizer st;
    public InputStream in;

    public PandaScanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(this.in = in));
    }

    public String nextLine() {
        try {
            return br.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }

    public String next() {
        if (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(nextLine().trim());
            return next();
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }


import java.io.*;
import java.util.*;
public class try23 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner k=new Scanner(System.in);
        int n=k.nextInt();
        int[][]dist0=new int[n][n];
        for (int i = 0; i < dist0.length; i++) {
            for (int j = 0; j < dist0.length; j++) {
                dist0[i][j]=k.nextInt();
            }
        }

        int road=k.nextInt();
        for (int i = 0; i < road; i++) {
            int first=k.nextInt();
            int second=k.nextInt();
            int cost=k.nextInt();
            int[][]dist1=new int[n][n];
            dist1[first-1][second-1]=cost;
            dist1[second-1][first-1]=cost;
            //for (int k1 = 0; k1 < dist1.length; k1++) {
                for (int ii = 0; ii < dist1.length; ii++) {
                    for (int j = 0; j < dist1.length; j++) {
                        int min=Math.min(dist0[ii][second-1]+dist0[first-1][j], dist0[ii][first-1]+dist0[second-1][j]);
                        dist1[ii][j]=Math.min(dist0[ii][j], min+cost);
                    }
                }
                long sum=0;
                boolean[][]check=new boolean[n][n];
                for (int j = 0; j < dist1.length; j++) {
                    for (int j2 = 0; j2 < dist1.length; j2++) {
                        if(!check[j][j2])
                        sum+=dist1[j][j2];

                        check[j][j2]=true;
                        check[j2][j]=true;
                        dist0[j][j2]=dist1[j][j2];
                    }
                }
            //}
                System.out.print(sum+" ");
        }
    }

}


import java.io.*;
import java.util.*;
public class try23 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner k=new Scanner(System.in);
        int n=k.nextInt();
        int[][]dist0=new int[n][n];
        for (int i = 0; i < dist0.length; i++) {
            for (int j = 0; j < dist0.length; j++) {
                dist0[i][j]=k.nextInt();
            }
        }

        int road=k.nextInt();
        for (int i = 0; i < road; i++) {
            int first=k.nextInt();
            int second=k.nextInt();
            int cost=k.nextInt();
            int[][]dist1=new int[n][n];
            dist1[first-1][second-1]=cost;
            dist1[second-1][first-1]=cost;
            //for (int k1 = 0; k1 < dist1.length; k1++) {
                for (int ii = 0; ii < dist1.length; ii++) {
                    for (int j = 0; j < dist1.length; j++) {
                        int min=Math.min(dist0[ii][second-1]+dist0[first-1][j], dist0[ii][first-1]+dist0[second-1][j]);
                        dist1[ii][j]=Math.min(dist0[ii][j], min+cost);
                    }
                }
                int sum=0;
                boolean[][]check=new boolean[n][n];
                for (int j = 0; j < dist1.length; j++) {
                    for (int j2 = 0; j2 < dist1.length; j2++) {
                        if(!check[j][j2])
                        sum+=dist1[j][j2];

                        check[j][j2]=true;
                        check[j2][j]=true;
                        dist0[j][j2]=dist1[j][j2];
                    }
                }
            //}
                System.out.print(sum+" ");
        }
    }

}


import java.io.*;
import java.util.*;

public class Problem25C
{
    public static void main (String [] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] matrix = new int[n+1][n+1];
        int[][] newmatrix = new int[n+1][n+1];
        for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) matrix[i][j] = in.nextInt();
        int k = in.nextInt();
        for(int i=1; i<=k; i++)
        {
            int a = in.nextInt();
            int b = in.nextInt();
            int l = in.nextInt();
            if(l<matrix[a][b])
            {
                matrix[a][b] = l;
                matrix[b][a] = l;
                for(int j=1; j<=n; j++) for(int m=1; m<=n; m++) newmatrix[j][m] = Math.min(matrix[j][m], Math.min(matrix[j][a]+matrix[a][b]+matrix[b][m], matrix[j][b]+matrix[b][a]+matrix[a][m]));
            }
            int sum = 0;
            for(int j=1; j<=n-1; j++) for(int m=j+1; m<=n; m++) sum+=newmatrix[j][m];
            if(i==k) System.out.print(sum);
            else System.out.print(sum + " ");
            for(int j=1; j<=n; j++) for(int m=1; m<=n; m++) matrix[j][m] = newmatrix[j][m];
        }
    }
}


import java.io.*;
import java.util.*;

public class Problem25C
{
    public static int n;
    public static int[][] matrix;

    public static void main (String [] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        matrix = new int[n+1][n+1];
        for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) matrix[i][j] = in.nextInt();
        int k = in.nextInt();
        for(int i=1; i<=k; i++)
        {
            int a = in.nextInt();
            int b = in.nextInt();
            int l = in.nextInt();
            if(l<matrix[a][b])
            {
                matrix[a][b] = l;
                matrix[b][a] = l;
                update(a, b);
            }
            int sum = 0;
            for(int j=1; j<=n-1; j++) for(int m=j+1; m<=n; m++) sum+=matrix[j][m];
            if(i==k) System.out.print(sum);
            else System.out.print(sum + " ");
        }
    }

    public static void update(int a, int b)
    {
        for(int j=1; j<=n; j++)
        {
            if(matrix[a][j]+matrix[a][b]<matrix[b][j])
            {
                matrix[b][j] = matrix[a][j]+matrix[a][b];
                matrix[j][b] = matrix[a][j]+matrix[a][b];
                update(b, j);
            }
        }
        for(int j=1; j<=n; j++)
        {
            if(matrix[b][j]+matrix[a][b]<matrix[a][j])
            {
                matrix[a][j] = matrix[b][j]+matrix[a][b];
                matrix[j][a] = matrix[b][j]+matrix[a][b];
                update(a, j);
            }
        }
    }
}


import java.io.*;
import java.util.*;

public class Problem25C
{
    public static void main (String [] args) throws IOException
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] matrix = new int[n+1][n+1];
        int[][] newmatrix = new int[n+1][n+1];
        for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) matrix[i][j] = in.nextInt();
        int k = in.nextInt();
        for(int i=1; i<=k; i++)
        {
            int a = in.nextInt();
            int b = in.nextInt();
            int l = in.nextInt();
            if(l<matrix[a][b])
            {
                matrix[a][b] = l;
                matrix[b][a] = l;
                for(int j=1; j<=n; j++) for(int m=1; m<=n; m++) newmatrix[j][m] = Math.min(matrix[j][m], Math.min(matrix[j][a]+matrix[a][b]+matrix[b][m], matrix[j][b]+matrix[b][a]+matrix[a][m]));
                for(int j=1; j<=n; j++) for(int m=1; m<=n; m++) matrix[j][m] = newmatrix[j][m];
            }
            int sum = 0;
            for(int j=1; j<=n-1; j++) for(int m=j+1; m<=n; m++) sum+=newmatrix[j][m];
            if(i==k) System.out.print(sum);
            else System.out.print(sum + " ");
        }
    }
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 3005;
#define inf 0x3f3f3f3f
#define exp 1e-6
struct dis
{
	int num, cost;
}s[maxn];
bool cmp(dis a, dis b)
{
	return a.num < b.num;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
		cin >> s[i].num;
	for (int i = 1;i <= n;i++)
		cin >> s[i].cost;
	sort(s + 1, s + 1 + n, cmp);
	ll sum, MIN = inf;
	int min = inf;
	for (int i = 2;i <= n - 1;i++)
	{
		sum = 0;
		for (int j = 1;j < i;j++)
			if (s[j].cost < min && s[j].num < s[i].num)
				min = s[j].cost;
		sum += min;
		min = inf;
		for (int k = i + 1;k <= n;k++)
			if (s[k].cost < min && s[k].num > s[i].num)
				min = s[k].cost;
		sum = sum + min + s[i].cost;
		if (sum < MIN)
			MIN = sum;
	}
	if (MIN == inf)
		cout << "-1" << endl;
	else
		cout << MIN << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 3010, inf = 0x3f3f3f3f;
int f[maxn][4], n, v[maxn], cost[maxn], ans = inf;

int main(){
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> v[i];
	for(int i = 1; i <= n; ++i) cin >> cost[i];
	memset(f, 0x3f, sizeof(f));
	for(int i = 1; i <= n; ++i){
		f[i][1] = cost[i];
		for(int k = 2; k <= 3; ++k)
			for(int j = 1; j < i; ++j)
				if(v[j] < v[i]) f[i][k] = min(f[i][k], f[j][k-1]+cost[i]);
	}
	for(int i = 1; i <= n; ++i) ans = min(ans, f[i][3]);
	printf("%d\n", ans == inf ? -1 : ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 3010, inf = 0xfffffff;
ll f[maxn][4], n, v[maxn], cost[maxn];
ll ans = inf;

int main(){
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> v[i];
	for(int i = 1; i <= n; ++i) cin >> cost[i];
	memset(f, 0x3f, sizeof(f));
	for(int i = 1; i <= n; ++i){
		f[i][1] = cost[i];
		for(int k = 2; k <= 3; ++k)
			for(int j = 1; j < i; ++j)
				if(v[j] < v[i]) f[i][k] = min(f[i][k], f[j][k-1]+cost[i]);
	}
	for(int i = 1; i <= n; ++i) ans = min(ans, f[i][3]);
	printf("%d\n", ans == inf ? -1 : ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 3007, inf = 0x3f3f3f3f;
int f[maxn][4], n, val[maxn], cost[maxn], ans = inf;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", val + i);
    for (int i = 1; i <= n; i++) scanf("%d", cost + i);
    memset(f, 0x3f, sizeof(f));
    for (int i = 1; i <= n; i++) {
        f[i][1] = cost[i];
        for (int k = 2; k <= 3; k++)
            for (int j = 1; j < i; j++)
                if (val[j] < val[i]) f[i][k] = std::min(f[i][k], f[j][k - 1] + cost[i]);
    }
    for (int i = 1; i <= n; i++) ans = std::min(ans, f[i][3]);
    printf("%d\n", ans == inf ? -1 : ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAXN 3010
#define INF 2100000000
typedef long long LL;
int s[MAXN],c[MAXN];
int read() {
	int ret=0;
	char c=getchar();
	while(c<'0' ||c>'9')c=getchar();
	do {
		ret=ret*10+c-'0';
		c=getchar();
	} while('0'<=c &&c<='9');
	return ret;
}
int n;
LL pre[MAXN],name[MAXN];
int main() {
	long long ans=INF;
	n=read();
	for(int i=1; i<=n; i++)s[i]=read();
	for(int i=1; i<=n; i++)c[i]=read();
	for(int i=2; i<=n; i++)pre[i]=INF;
	for(int i=3; i<=n; i++) {
		for(int j=i-1; j>=2; j--) {
			if(s[i] <= s[j])continue;
			for(int k=j-1; k>=1; k--) {
				if(s[j] <= s[k])continue;
				ans=min(ans,(LL)c[i]+c[j]+c[k]);
			}
		}
	}
	if(ans==INF)printf("-1\n");
	else printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAXN 3010
#define INF 2100000000
typedef long long LL;
int s[MAXN],c[MAXN];
int read() {
	int ret=0;
	char c=getchar();
	while(c<'0' ||c>'9')c=getchar();
	do {
		ret=ret*10+c-'0';
		c=getchar();
	} while('0'<=c &&c<='9');
	return ret;
}
int n;
LL pre[MAXN],name[MAXN];
int main() {
	long long ans=INF;
	n=read();
	for(int i=1; i<=n; i++)s[i]=read();
	for(int i=1; i<=n; i++)c[i]=read();
	for(int i=2; i<=n; i++)pre[i]=INF;
	for(int i=2; i<=n; i++) {
		for(int j=1; j<i; j++)
			if(s[j]<s[i] && c[j] < pre[i] && name[j]!=0) {
				pre[i]=c[j];
				name[i]=j;
				ans=min(ans,c[i]+pre[i]+pre[name[i]]);
//				printf("i=%d j=%d pre=%lld c=%d ans=%lld\n",i,j,pre[i],c[j],ans);
			}
		for(int j=1; j<i; j++)
			if(s[j] < s[i] && c[j] < pre[i]) {
				pre[i]=c[j];
				name[i]=j;
			}
	}
	if(ans==INF)printf("-1\n");
	else printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 3010
#define INF 2100000000
typedef long long LL;
int s[MAXN],c[MAXN];
int read() {
	int ret=0;
	char c=getchar();
	while(c<'0' ||c>'9')c=getchar();
	do {
		ret=ret*10+c-'0';
		c=getchar();
	} while('0'<=c &&c<='9');
	return ret;
}
int n;
LL pre[MAXN],name[MAXN];
int main() {
	LL ans=2147483640*3LL;
	n=read();
	for(int i=1; i<=n; i++)s[i]=read();
	for(int i=1; i<=n; i++)c[i]=read();
	for(int i=1; i<=n; i++)pre[i]=2147483640*2LL;
	for(int i=2; i<=n; i++)
		for(int j=1; j<i; j++)
			if(s[i] > s[j])
				pre[i]=min(pre[i],(LL)c[i]+c[j]);
	for(int i=3; i<=n; i++)
		for(int j=1; j<i; j++)
			if(s[i] > s[j])
				ans=min(ans,c[i]+pre[j]);
	if(ans==2147483640*3LL)printf("-1\n");
	else printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 3010
#define INF 2100000000
typedef long long LL;
int s[MAXN],c[MAXN];
int read() {
	int ret=0;
	char c=getchar();
	while(c<'0' ||c>'9')c=getchar();
	do {
		ret=ret*10+c-'0';
		c=getchar();
	} while('0'<=c &&c<='9');
	return ret;
}
int n;
LL pre1[MAXN],name[MAXN];
int main() {
	LL ans=2147483640*3LL;
	n=read();
	for(int i=1; i<=n; i++)s[i]=read();
	for(int i=1; i<=n; i++)c[i]=read();
	for(int i=1; i<=n; i++)pre1[i]=2147483640*2LL;
	for(int i=2; i<=n; i++)
		for(int j=1; j<i; j++)
			if(s[i] > s[j])
				pre1[i]=min(pre1[i],(LL)c[i]+c[j]);
	for(int i=3; i<=n; i++)
		for(int j=1; j<i; j++)
			if(s[i] > s[j])
				ans=min(ans,c[i]+pre1[j]);
	if(ans==2147483640*3LL)printf("-1\n");
	else printf("%lld\n",ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
#define MAXN 3010
#define INF 2100000000
typedef long long LL;
int s[MAXN],c[MAXN];
int read() {
	int ret=0;
	char c=getchar();
	while(c<'0' ||c>'9')c=getchar();
	do {
		ret=ret*10+c-'0';
		c=getchar();
	} while('0'<=c &&c<='9');
	return ret;
}
struct fff {
	int id,val;
};
bool operator < (fff  a,fff b) {
	return (a.val > b.val) || (a.val==b.val && a.id < b.id);
}
priority_queue<fff> hhh;
int n;
LL pre[MAXN],name[MAXN];
int main() {
	long long ans=INF;
	fff test;
	n=read();
	for(int i=1; i<=n; i++)s[i]=read();
	for(int i=1; i<=n; i++)c[i]=read();
	for(int i=2; i<=n; i++)pre[i]=INF;
	for(int i=2; i<=n; i++) {
		if(i>=3) {
//			printf("id=%d val=%d\n",hhh.top().id,hhh.top().val);
			while(!hhh.empty() && hhh.top().id >= s[i]) {
				hhh.pop();
			}
			if(!hhh.empty())ans=min(ans,(LL)c[i]+hhh.top().val);
		}
		for(int j=1; j<i; j++)
			if(s[i] > s[j]) {
				test.id=s[i];
				test.val=c[j]+c[i];
				hhh.push(test);
			}
	}
	if(ans==INF)printf("-1\n");
	else printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int dp[3000][3000];
int main()
{
    int n;
    int s[5000],c[5000];
    int mi[5000];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);

    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
          dp[i][j]=-1;

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(s[i]<s[j])
            {
                dp[i][j]=c[j]+c[i];
            }
            else
                dp[i][j]==-1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        sort(dp[i],dp[i]+1+n);
        for(int j=1;j<=n;j++)
            if(dp[i][j]!=-1)
        {
            mi[i]=dp[i][j];
            break;
        }
    }
    int ma1=0;
    for(int i=1;i<=n-2;i++)
     {
         int ma=0;
         for(int j=i+1;j<n;j++)
         {
             if(s[i]<s[j])
             {
                    if(ma==0)
                        ma=mi[j];
                    else if(ma!=0&&ma>mi[j])
                        ma=mi[j];
             }
         }

         if(ma!=0)
         {
             if(ma1==0)
                ma1=ma+c[i];
             else if(ma1>ma+c[i])
             ma1=ma+c[i];

         }
     }
     if(ma1==0)
        printf("-1");
     else
     printf("%d",ma1);
   return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int dp[3010][3010];
int main()
{
    int n;
    int s[5000],c[5000];
    int mi[5000];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);

    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
          dp[i][j]=-1;

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(s[i]<s[j])
            {
                dp[i][j]=c[j]+c[i];
            }
            else
                dp[i][j]==-1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        sort(dp[i],dp[i]+1+n);
        for(int j=1;j<=n;j++)
            if(dp[i][j]!=-1)
        {
            mi[i]=dp[i][j];
            break;
        }
    }
   /* for(int i=1;i<=n;i++)
       printf("%d ",mi[i]);
       cout<<endl;*/
    int ma1=0;
    for(int i=1;i<=n-2;i++)
     {
         int ma=0;
         for(int j=i+1;j<n;j++)
         {
             if(s[i]<s[j]&&mi[j]!=0)
             {
                    if(ma==0)
                        ma=mi[j];
                    else if(ma!=0&&ma>mi[j])
                        ma=mi[j];
             }
         }

         if(ma!=0)
         {
             if(ma1==0)
                ma1=ma+c[i];
             else if(ma1>ma+c[i])
             ma1=ma+c[i];

         }
     }
     if(ma1==0)
        printf("-1");
     else
     printf("%d",ma1);
   return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int dp[3000][3000];
int main()
{
    int n;
    int s[5000],c[5000];
    int mi[5000];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);

    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
          dp[i][j]=-1;

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(s[i]<s[j])
            {
                dp[i][j]=c[j]+c[i];
            }
            else
                dp[i][j]==-1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        sort(dp[i],dp[i]+1+n);
        for(int j=1;j<=n;j++)
            if(dp[i][j]!=-1)
        {
            mi[i]=dp[i][j];
            break;
        }
    }
    for(int i=1;i<=n;i++)
       printf("%d ",mi[i]);
       cout<<endl;
    int ma1=0;
    for(int i=1;i<=n-2;i++)
     {
         int ma=0;
         for(int j=i+1;j<n;j++)
         {
             if(s[i]<s[j]&&mi[j]!=0)
             {
                    if(ma==0)
                        ma=mi[j];
                    else if(ma!=0&&ma>mi[j])
                        ma=mi[j];
             }
         }

         if(ma!=0)
         {
             if(ma1==0)
                ma1=ma+c[i];
             else if(ma1>ma+c[i])
             ma1=ma+c[i];

         }
     }
     if(ma1==0)
        printf("-1");
     else
     printf("%d",ma1);
   return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
    int n;
    int s[5000],c[5000];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);
    int dp[10];
    dp[0]=0;
    memset(dp,0,sizeof(dp));
    int dpp=0;
    for(int i=1;i<=n-2;i++)
    {
         dp[0]=c[i];

         for(int j=i+1;j<=n-1;j++)
         {
             int dp1=0;
             if(s[i]<s[j])
             {
                dp1=dp[0]+c[j];
                int dp2=0;
                for(int k=j+1;k<=n;k++)
                {
                    int dp21=0;
                    if(s[j]<s[k])
                    dp21=dp1+c[k];
                    if(dp2==0)
                        dp2=dp21;
                    else if(dp2>dp21&&dp21!=0)
                        dp2=dp21;
                }
                if(dp[2]==0)
                {
                    dp[2]=dp2;
                }
                else if(dp2!=0&&dp2<dp[2])
                    dp[2]=dp2;
             }
         }
         if(dpp==0)
            dpp=dp[2];
         else if(dp[2]!=0&&dp[2]<dpp)
            dpp=dp[2];
    }
    if(dpp==0)
        printf("-1");
    else
        printf("%d",dpp);
   return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int dp[3000][3000];
int main()
{
    int n;
    int s[5000],c[5000];
    int mi[5000];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&s[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);

    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
          dp[i][j]=-1;

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(s[i]<s[j])
            {
                dp[i][j]=c[j]+c[i];
            }
            else
                dp[i][j]==-1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        sort(dp[i],dp[i]+1+n);
        for(int j=1;j<=n;j++)
            if(dp[i][j]!=-1)
        {
            mi[i]=dp[i][j];
            break;
        }
    }
   /* for(int i=1;i<=n;i++)
       printf("%d ",mi[i]);
       cout<<endl;*/
    int ma1=0;
    for(int i=1;i<=n-2;i++)
     {
         int ma=0;
         for(int j=i+1;j<n;j++)
         {
             if(s[i]<s[j]&&mi[j]!=0)
             {
                    if(ma==0)
                        ma=mi[j];
                    else if(ma!=0&&ma>mi[j])
                        ma=mi[j];
             }
         }

         if(ma!=0)
         {
             if(ma1==0)
                ma1=ma+c[i];
             else if(ma1>ma+c[i])
             ma1=ma+c[i];

         }
     }
     if(ma1==0)
        printf("-1");
     else
     printf("%d",ma1);
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mx 1e11

int main()
{
	ll n,i,j;
	cin>>n;
	ll a[n],b[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n;i++)
	cin>>b[i];
	ll dp[n];
	ll x[n],y[n];

	for(i=0;i<n;i++)
	dp[i]=mx,x[i]=-1,y[i]=-1;
	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
		{
			if(a[i]>a[j] and dp[i]>b[j])
			{
				dp[i]=b[j];
				x[i]=j;
			}
		}
		//cout<<dp[i]<<" ";
	}
	ll dp1[n];

	for(i=0;i<n;i++)
	dp1[i]=mx;

	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
		{
			if(a[i]>a[j] and j!=x[i])
			{
				if(y[i]!=-1)
				dp1[i]=min(dp1[i],b[j]+dp[j]);
				else
				dp1[i]=b[j]+dp[j];
				y[i]=1;
			}
		}
		//cout<<dp1[i]<<" ";
	}
	ll mn=INT_MAX;
	for(i=0;i<n;i++)
	{
		if(x[i]!=-1 and y[i]!=-1 and mn>dp1[i]+b[i])
		mn=dp1[i]+b[i];
	}
	if(mn<INT_MAX)
	cout<<mn;
	else
	cout<<-1;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mx 1e14

int main()
{
	ll n,i,j;
	cin>>n;
	ll a[n],b[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n;i++)
	cin>>b[i];
	ll dp[n];
	ll x[n],y[n];

	for(i=0;i<n;i++)
	dp[i]=mx,x[i]=-1,y[i]=-1;
	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
		{
			if(a[i]>a[j] and dp[i]>b[j])
			{
				dp[i]=b[j];
				x[i]=j;
			}
		}
		//cout<<dp[i]<<" ";
	}
	ll dp1[n];

	for(i=0;i<n;i++)
	dp1[i]=mx;

	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
		{
			if(a[i]>a[j])
			{
				if(y[i]!=-1)
				dp1[i]=min(dp1[i],b[j]+dp[j]);
				else
				dp1[i]=b[j]+dp[j];
				y[i]=1;
			}
		}
		//cout<<dp1[i]<<" ";
	}
	ll mn=mx;
	for(i=0;i<n;i++)
	{
		if(x[i]!=-1 and y[i]!=-1 and mn>dp1[i]+b[i])
		mn=dp1[i]+b[i];
	}
	if(mn<mx)
	cout<<mn;
	else
	cout<<-1;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mx 1e11

int main()
{
	ll n,i,j;
	cin>>n;
	ll a[n],b[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n;i++)
	cin>>b[i];
	ll dp[n];
	ll x[n],y[n];

	for(i=0;i<n;i++)
	dp[i]=mx,x[i]=-1,y[i]=-1;
	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
		{
			if(a[i]>a[j] and dp[i]>b[j])
			{
				dp[i]=b[j];
				x[i]=j;
			}
		}
		//cout<<dp[i]<<" ";
	}
	ll dp1[n];

	for(i=0;i<n;i++)
	dp1[i]=mx;

	for(i=0;i<n;i++)
	{
		for(j=0;j<i;j++)
		{
			if(a[i]>a[j] and j!=x[i])
			{
				if(y[i]!=-1)
				dp1[i]=min(dp1[i],b[j]+dp[j]);
				else
				dp1[i]=b[j]+dp[j];
				y[i]=1;
			}
		}
		//cout<<dp1[i]<<" ";
	}
	ll mn=mx;
	for(i=0;i<n;i++)
	{
		if(x[i]!=-1 and y[i]!=-1 and mn>dp1[i]+b[i])
		mn=dp1[i]+b[i];
	}
	if(mn<mx)
	cout<<mn;
	else
	cout<<-1;

}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

#define Rep(i,l,r) for (int i = (l); i <= (r); ++ i)
#define RepD(i,r,l) for (int i = (r); i >= (l); -- i)
#define RepG(i,x) for (int i = hd[x]; i; i = e[i].nxt)

const int N = 5005;
const int inf = 1e9+7;

int n, pre[N], suf[N], c[N];
struct data {int x, y, id;} a[N];

inline int cmp (data a, data b) {return a.x==b.x? a.id>b.id: a.x<b.x;}

inline void mbit (int o, int x) {
	while (o<=n) c[o] = min (c[o], x), o += (o&-o);
}

inline int qbit (int o) {
	int res = inf;
	while (o) res = min (res, c[o]), o -= (o&-o);
	return res;
}

int main () {
	int ans = inf;
	scanf ("%d", &n);
	Rep(i,1,n) scanf ("%d", &a[i].x);
	Rep(i,1,n) scanf ("%d", &a[i].y), a[i].id = i;
	sort (a+1, a+n+1, cmp);
	Rep(i,1,n) c[i] = inf;
	Rep(i,1,n) pre[a[i].id] = qbit (a[i].id), mbit (a[i].id, a[i].y);
	int li = n>>1;
	Rep(i,1,li) swap (a[i], a[n-i+1]);
	Rep(i,1,n) c[i] = inf;
	Rep(i,1,n) suf[a[i].id] = qbit (n-a[i].id+1), mbit (n-a[i].id+1, a[i].y);
	Rep(i,1,n) ans = min (ans, pre[a[i].id]+a[i].y+suf[a[i].id]);
	if (ans==inf) ans = -1;
	printf ("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

#define Rep(i,l,r) for (int i = (l); i <= (r); ++ i)
#define RepD(i,r,l) for (int i = (r); i >= (l); -- i)
#define RepG(i,x) for (int i = hd[x]; i; i = e[i].nxt)

const int N = 5005;
const int inf = 1e9+7;

int n, pre[N], suf[N], c[N];
struct data {int x, y, id;} a[N];

inline int cmp1 (data a, data b) {return a.x==b.x? a.id>b.id: a.x<b.x;}

inline void mbit (int o, int x) {
	while (o<=n) c[o] = min (c[o], x), o += (o&-o);
}

inline int qbit (int o) {
	int res = inf;
	while (o) res = min (res, c[o]), o -= (o&-o);
	return res;
}

int main () {
	int ans = inf;
	scanf ("%d", &n);
	Rep(i,1,n) scanf ("%d", &a[i].x);
	Rep(i,1,n) scanf ("%d", &a[i].y), a[i].id = i;
	sort (a+1, a+n+1, cmp1);
	Rep(i,1,n) c[i] = inf;
	Rep(i,1,n) pre[a[i].id] = qbit (a[i].id), mbit (a[i].id, a[i].y);
	int li = n>>1;
	Rep(i,1,li) swap (a[i], a[n-i+1]);
	Rep(i,1,n) c[i] = inf;
	Rep(i,1,n) suf[a[i].id] = qbit (n-a[i].id+1), mbit (n-a[i].id+1, a[i].y);
	Rep(i,1,n) ans = min (ans, pre[a[i].id]+a[i].y+suf[a[i].id]);
	if (ans==inf) ans = -1;
	printf ("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

#define Rep(i,l,r) for (int i = (l); i <= (r); ++ i)
#define RepD(i,r,l) for (int i = (r); i >= (l); -- i)
#define RepG(i,x) for (int i = hd[x]; i; i = e[i].nxt)

const int N = 5005;
const int inf = 1e9+7;

int n, pre[N], suf[N], c[N];
struct data {int x, y, id;} a[N];

inline int cmp1 (data a, data b) {return a.x==b.x? a.id>b.id: a.x<b.x;}
inline int cmp2 (data a, data b) {return a.x==b.x? a.id<b.id: a.x>b.x;}
inline int cmp3 (data a, data b) {return a.id<b.id;}

inline void mbit (int o, int x) {
	while (o<=n) c[o] = min (c[o], x), o += (o&-o);
}

inline int qbit (int o) {
	int res = inf;
	while (o) res = min (res, c[o]), o -= (o&-o);
	return res;
}

int main () {
	int ans = inf;
	scanf ("%d", &n);
	Rep(i,1,n) scanf ("%d", &a[i].x);
	Rep(i,1,n) scanf ("%d", &a[i].y), a[i].id = i;
	sort (a+1, a+n+1, cmp1);
	Rep(i,1,n) c[i] = inf;
	Rep(i,1,n) pre[a[i].id] = qbit (a[i].id), mbit (a[i].id, a[i].y);
	sort (a+1, a+n+1, cmp2);
	Rep(i,1,n) c[i] = inf;
	Rep(i,1,n) suf[a[i].id] = qbit (n-a[i].id+1), mbit (n-a[i].id+1, a[i].y);
	sort (a+1, a+n+1, cmp3);
	Rep(i,1,n) ans = min (ans, pre[i]+a[i].y+suf[i]);
	if (ans==inf) ans = -1;
	printf ("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

#define inf 0x3f3f3f3f

int main()
{
    int dp[3005][4];
    int n,len[3005],cost[3005];
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> len[i];
    for(int i = 1; i <= n; i++)
        cin >> cost[i];
    memset(dp,inf,sizeof(dp));
    for(int i = 1; i <= n; i++)
    {
        dp[i][1]=cost[i];
        for(int k = 2; k<= 3; k++)
            for(int j = 1; j < i; j++)
                if(len[j]<len[i])
                    dp[i][k]=min(dp[i][k],dp[j][k-1]+cost[i]);
    }
    int cnt=inf;
    for(int i = 1; i <= n;i++)
        cnt=min(cnt,dp[i][3]);
    if(cnt==inf)
        cout << -1 << endl;
    else cout << cnt << endl;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
#define inf 0x3f3f3f3f

int main()
{
    int dp[3001][5];
    int n,len[3001],cost[1001];
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> len[i];
    for(int i = 1; i <= n; i++)
        cin >> cost[i];
    memset(dp,inf,sizeof(dp));
    for(int i = 1; i <= n; i++)
    {
        dp[i][1]=cost[i];
        for(int j = 2; j<= 3; j++)
        {
            for(int k = 1; k <= i; k++)
            if(len[k]<len[i])
                dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[i]);
        }
    }
    int cnt=inf;
    for(int i = 1; i <= n;i++)
        cnt=min(cnt,dp[i][3]);
    if(cnt==inf)
        cout << -1 << endl;
    else cout << cnt << endl;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
#define inf 0x3f3f3f3f

int main()
{
    int dp[3001][5];
    int n,len[3001],cost[1001];
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> len[i];
    for(int i = 1; i <= n; i++)
        cin >> cost[i];
    memset(dp,inf,sizeof(dp));
    for(int i = 1; i <= n; i++)
    {
        dp[i][1]=cost[i];
        for(int j = 2; j<= 3; j++)
        {
            for(int k = 1; k < i; k++)
            if(len[k]<len[i])
                dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[i]);
        }
    }
    int cnt=inf;
    for(int i = 1; i <= n;i++)
        cnt=min(cnt,dp[i][3]);
    if(cnt==inf)
        cout << -1 << endl;
    else cout << cnt << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 3007, inf = 0x3f3f3f3f;
int f[maxn][4], n, val[maxn], cost[maxn], ans = inf;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", val + i);
    for (int i = 1; i <= n; i++) scanf("%d", cost + i);
    memset(f, 0x3f, sizeof(f));
    for (int i = 1; i <= n; i++) {
        f[i][1] = cost[i];
        for (int k = 2; k <= 3; k++)
            for (int j = 1; j < i; j++)
                if (val[j] < val[i]) f[i][k] = std::min(f[i][k], f[j][k - 1] + cost[i]);
    }
    for (int i = 1; i <= n; i++) ans = std::min(ans, f[i][3]);
    printf("%d\n", ans == inf ? -1 : ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define  inf  0x3f3f3f3f
int f[3005][4],n;
int val[3005],cost[3005],ans=inf;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", val[i]);
    for (int i = 1; i <= n; i++)
        scanf("%d", cost[i]);
    memset(f, 0x3f, sizeof(f));
    for (int i = 1; i <= n; i++)
    {
        f[i][1] = cost[i];
        for (int k = 2; k <= 3; k++)
            for (int j = 1; j < i; j++)
                if (val[j] < val[i]) f[i][k] =(f[i][k], f[j][k - 1] + cost[i]);
    }
    for (int i = 1; i <= n; i++)
        ans = min(ans, f[i][3]);
    printf("%d\n", ans == inf ? -1 : ans);
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    int arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    int maxCost = INT_MAX;
    f(i,0,n)
    {
        cin>>cost[i];
    }
    f(i,0,n)
    {
        minL[i] = INT_MAX;
        maxR[i] = INT_MAX;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = (INT_MAX*(long)3);

    f(i,1,n-1)
    {
        if(ans > (cost[i]+minL[i]+maxR[i]))
            ans = cost[i]+minL[i]+maxR[i];
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    long arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    f(i,0,n)
    {
        cin>>cost[i];

    }
    long maxCost=cost[0];
    f(i,0,n)
        maxCost = max(maxCost, cost[i]);
    f(i,0,n)
    {
        minL[i] = maxCost;
        maxR[i] = maxCost;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = maxCost*(long)3;
    // cout << "xxxxxxx "<<INT_MAX*3<<endl;

    f(i,1,n-1)
    {
        long sum = (cost[i]+minL[i]+maxR[i]);
        if(ans > sum && (sum >= 0))
            ans = sum;
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    long arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    int maxCost = INT_MAX;
    f(i,0,n)
    {
        cin>>cost[i];
    }
    f(i,0,n)
    {
        minL[i] = INT_MAX;
        maxR[i] = INT_MAX;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = (INT_MAX*(long)3);

    f(i,1,n-1)
    {
        if(ans > (cost[i]+minL[i]+maxR[i]))
            ans = cost[i]+minL[i]+maxR[i];
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    long arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    int maxCost = INT_MAX;
    f(i,0,n)
    {
        cin>>cost[i];
    }
    f(i,0,n)
    {
        minL[i] = INT_MAX;
        maxR[i] = INT_MAX;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = (INT_MAX*(long)3);

    f(i,1,n-1)
    {
        if(ans > (cost[i]+minL[i]+maxR[i]))
            ans = cost[i]+minL[i]+maxR[i];
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    long arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    long maxCost = INT_MAX;
    f(i,0,n)
    {
        cin>>cost[i];
    }
    f(i,0,n)
    {
        minL[i] = INT_MAX;
        maxR[i] = INT_MAX;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = (INT_MAX*(long)3);

    f(i,1,n-1)
    {
        if(ans > (cost[i]+minL[i]+maxR[i]))
            ans = cost[i]+minL[i]+maxR[i];
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    int arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    int maxCost = INT_MAX;
    f(i,0,n)
    {
        cin>>cost[i];
    }
    f(i,0,n)
    {
        minL[i] = INT_MAX;
        maxR[i] = INT_MAX;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = INT_MAX;

    f(i,1,n-1)
    {
        if(ans > (cost[i]+minL[i]+maxR[i]))
            ans = cost[i]+minL[i]+maxR[i];
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    long arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    long maxCost = INT_MAX;
    f(i,0,n)
    {
        cin>>cost[i];
		maxCost = max(maxCost, cost[i]);
    }
    f(i,0,n)
    {
        minL[i] = INT_MAX;
        maxR[i] = INT_MAX;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = (INT_MAX*(long)3);

    f(i,1,n-1)
    {
        if(ans > (cost[i]+minL[i]+maxR[i]))
            ans = cost[i]+minL[i]+maxR[i];
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <sstream>
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define MOD 1000000007
//#define MUL(a,b) ((a)%MOD*(b)%MOD)%MOD
#define MUL(a,b) ((long long)(((long long)(a)*(b))%MOD))
#define SQR(a) MUL(a,a)
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define PI 3.1415926535897932384626433832795
#define sl(x) scanf("%lld",&x)
#define f(i,a,b) for(int i=a;i<b;i++)
typedef long long LL;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.sync_with_stdio(0);
    //while(1){
    int n; cin>>n;
    long arr[n],cost[n],minL[n],maxR[n];
    f(i,0,n)
        cin>>arr[i];
    long maxCost = INT_MAX;
    f(i,0,n)
    {
        cin>>cost[i];
		maxCost = max(maxCost, cost[i]);
    }
    f(i,0,n)
    {
        minL[i] = maxCost;
        maxR[i] = maxCost;
    }
    bool leftFlag = false, rightFlag = false;
    f(i,1,n-1)
    {
        // calc left
        int minArr = arr[i], minCostL = maxCost;
        for(int j=i-1;j>=0;j--)
        {
            if(arr[j]<arr[i] && cost[j] <= minCostL)
            {
                leftFlag = true;
                minArr = arr[j]; minCostL = cost[j];
            }
        }
        minL[i] = minCostL;

        // calc right
        minArr = arr[i]; int minCostR = maxCost;
        for(int j=i+1; j<n; j++)
        {
            if(arr[j]>arr[i] && cost[j] <= minCostR)
            {
                rightFlag = true;
                minArr = arr[j]; minCostR = cost[j];
            }
        }
        maxR[i] = minCostR;
    }
    long ans = (INT_MAX*(long)3);

    f(i,1,n-1)
    {
        if(ans > (cost[i]+minL[i]+maxR[i]))
            ans = cost[i]+minL[i]+maxR[i];
    }
    if(!leftFlag || !rightFlag)
        cout <<"-1"<<endl;
    else cout << ans <<endl;
    //}
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int INF = 2000000000;
int n;
int s[3003];
int c[3003];
int main() {
	cin >> n;
	for(int i = 1 ; i <= n ; ++i) scanf("%d",&s[i]);
	for(int i = 1 ; i <= n ; ++i) scanf("%d",&c[i]);
	int ans = INF;
	for(int i = 2 ; i < n ; ++i) {
		int min1 = INF;
		for(int j = 1 ; j < i ; ++j) {
			if(s[j]<s[i]) min1 = min(min1,c[j]);
		}
		if(min1==INF) continue;
		int min2 = INF;
		for(int j = i+1 ; j < n ; ++j) {
			if(s[i]<s[j]) min2 = min(min2,c[j]);
		}
		if(min2==INF) continue;
		ans = min(ans,min1+min2+c[i]);
	}

	for(int i = 2 ; i < n ; ++i) {
		int min1 = INF;
		for(int j = 1 ; j < i ; ++j) {
			if(s[j]>s[i]) min1 = min(min1,c[j]);
		}
		if(min1==INF) continue;
		int min2 = INF;
		for(int j = i+1 ; j < n ; ++j) {
			if(s[i]>s[j]) min2 = min(min2,c[j]);
		}
		if(min2==INF) continue;
		ans = min(ans,min1+min2+c[i]);
	}
	if(ans==INF) printf("-1\n");
	else printf("%d\n",ans);

}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int INF = 2000000000;
int n;
int s[3003];
int c[3003];
int main() {
	cin >> n;
	for(int i = 1 ; i <= n ; ++i) scanf("%d",&s[i]);
	for(int i = 1 ; i <= n ; ++i) scanf("%d",&c[i]);
	int ans = INF;
	for(int i = 2 ; i < n ; ++i) {
		int min1 = INF;
		for(int j = 1 ; j < i ; ++j) {
			if(s[j]<s[i]) min1 = min(min1,c[j]);
		}
		if(min1==INF) continue;
		int min2 = INF;
		for(int j = i+1 ; j < n ; ++j) {
			if(s[i]<s[j]) min2 = min(min2,c[j]);
		}
		if(min2==INF) continue;
		ans = min(ans,min1+min2+c[i]);
	}
	if(ans==INF) printf("-1\n");
	else printf("%d\n",ans);

}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int INF = 2000000000;
int n;
int s[3003];
int c[3003];
int main() {
	cin >> n;
	for(int i = 1 ; i <= n ; ++i) scanf("%d",&s[i]);
	for(int i = 1 ; i <= n ; ++i) scanf("%d",&c[i]);
	int ans = INF;
	for(int i = 2 ; i < n ; ++i) {
		int min1 = INF;
		for(int j = 1 ; j < i ; ++j) {
			if(s[j]<s[i]) min1 = min(min1,c[j]);
		}
		if(min1==INF) continue;
		int min2 = INF;
		for(int j = i+1 ; j <= n ; ++j) {
			if(s[i]<s[j]) min2 = min(min2,c[j]);
		}
		if(min2==INF) continue;
		ans = min(ans,min1+min2+c[i]);
	}
	if(ans==INF) printf("-1\n");
	else printf("%d\n",ans);

}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()

using namespace std;

const ll INF=1LL*1001*1001*1001*1001*1001*1001;

int n;
ll s[30003];
ll c[30003];

int main()
{
          //freopen("INPUT.txt","r",stdin);
          ios_base::sync_with_stdio(0);
          cin>>n;
          for(int i=0;i<n;i++)
                    cin>>s[i];
          for(int i=0;i<n;i++)
                    cin>>c[i];

          ll ans=INF;
          for(int i=1;i<n-1;i++)
          {
                    ll mn1=INF;
                    ll mn2=INF;
                    for(int j=0;j<i;j++)
                              if(s[j]<s[i])
                                        mn1=min(mn1,c[j]);
                    for(int j=i+1;j<n;j++)
                              if(s[j]>s[i])
                                        mn2=min(mn2,c[j]);
                    if(mn1==INF || mn2==INF)
                              continue;
                    ans=min(ans,mn1+mn2+c[i]);
          }
          if(ans==INF)
                    cout<<-1;
          else
                    cout<<ans;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()

using namespace std;

const ll INF=1LL*1001*1001*1001*1001*1001*1001;

int n;
ll s[30003];
ll c[30003];

int main()
{
          //freopen("INPUT.txt","r",stdin);
          ios_base::sync_with_stdio(0);
          cin>>n;
          for(int i=0;i<n;i++)
                    cin>>s[i];
          for(int i=0;i<n;i++)
                    cin>>c[i];

          ll ans=INF;
          for(int i=1;i<n-1;i++)
          {
                    ll mn1=INF;
                    ll mn2=INF;
                    for(int j=0;j<i;j++)
                              if(s[j]<s[i])
                                        mn1=min(mn1,c[j]);
                    for(int j=i+1;j<n-1;j++)
                              if(s[j]>s[i])
                                        mn2=min(mn2,c[j]);
                    if(mn1==INF || mn2==INF)
                              continue;
                    ans=min(ans,mn1+mn2+c[i]);
          }
          if(ans==INF)
                    cout<<-1;
          else
                    cout<<ans;
}