	import java.util.*;

import java.io.*;
	import java.math.*;
	import java.awt.geom.*;

	import static java.lang.Math.*;

	public class Solution implements Runnable {

	    public void solve() throws Exception {

//	    	int t=sc.nextInt();
//	    	long mod=(long)1e9+7;
//
//	    	while(t-->0)
//	    	{
	    		int n=sc.nextInt();
	    		int m=sc.nextInt();
	    		long arr[]=new long[n];

	    		for(int i=0;i<n;i++)
	    		{
	    			arr[i]=sc.nextLong();
	    		}

	    		for(int i=1;i<n;i++)
	    		{

	    			arr[i]=arr[i]+arr[i-1];
	    			out.println(arr[i]);
	    		}
	    		for(int i=0;i<m;i++)
	    		{
	    			long x=sc.nextLong();
	    			int start=0;
	    			int end=n-1;
	    			if(start==end)
	    			{
	    				out.println(1+" "+x);
	    				continue;
	    			}
	    			if(x>=arr[end])
    				{

    					out.println((end+1)+" "+(arr[end]-((end==0)?0:arr[end-1])));
    					continue;
    				}
	    			if(x<=arr[start])
    				{

    					out.println((1)+" "+x);
    					continue;
    				}
	    			while(start<=end)
	    			{
	    				int mid=start+(end-start)/2;
	    				if(x==arr[mid])
	    				{
	    					out.println((mid+1)+" "+(arr[mid]));
	    					break;
	    				}
	    				if(mid+1<n)
	    				{
	    				if(x>=arr[mid] && x<arr[mid+1])
	    				{
	    					out.println((mid+2)+" "+(x-arr[mid]));
	    					break;
	    				}
	    				}
	    				if(x<arr[mid])
	    				{
	    					end=mid-1;
	    				}
	    				else if(x>arr[mid] && x>arr[mid+1])
	    				{
	    					start=mid+1;
	    				}
	    			}
	    		}

	    }

	    static long power(long x, long y, long p)
	    {

	        long res = 1;

	        x = x % p;

	       if (x == 0) return 0;

	        while (y > 0)
	        {
	            if((y & 1)==1)
	                res = (res * x) % p;
	            y = y >> 1;
	            x = (x * x) % p;
	        }
	        return res;
	    }

	    static Throwable uncaught;

	    BufferedReader in;
	    FastScanner sc;
	    PrintWriter out;

	    @Override
	    public void run() {
	        try {
	            in = new BufferedReader(new InputStreamReader(System.in));
	            out = new PrintWriter(System.out);
	            sc = new FastScanner(in);
	            solve();
	        } catch (Throwable uncaught) {
	            Solution.uncaught = uncaught;
	        } finally {
	            out.close();
	        }
	    }

	    public static void main(String[] args) throws Throwable {
	        Thread thread = new Thread(null, new Solution(), "", (1 << 26));
	        thread.start();
	        thread.join();
	        if (Solution.uncaught != null) {
	            throw Solution.uncaught;
	        }
	    }

	}

	class FastScanner {

	    BufferedReader in;
	    StringTokenizer st;

	    public FastScanner(BufferedReader in) {
	        this.in = in;
	    }

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

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

	    public int[] readArray(int n) throws Exception {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

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

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

	}


	import java.util.*;

import java.io.*;
	import java.math.*;
	import java.awt.geom.*;

	import static java.lang.Math.*;

	public class Solution implements Runnable {

	    public void solve() throws Exception {

//	    	int t=sc.nextInt();
//	    	long mod=(long)1e9+7;
//
//	    	while(t-->0)
//	    	{
	    		int n=sc.nextInt();
	    		int m=sc.nextInt();
	    		long arr[]=new long[n];

	    		for(int i=0;i<n;i++)
	    		{
	    			arr[i]=sc.nextLong();
	    		}

	    		for(int i=1;i<n;i++)
	    		{

	    			arr[i]=arr[i]+arr[i-1];
	    			//out.println(arr[i]);
	    		}
	    		for(int i=0;i<m;i++)
	    		{
	    			long x=sc.nextLong();
	    			int start=0;
	    			int end=n-1;
	    			if(start==end)
	    			{
	    				out.println(1+" "+x);
	    				continue;
	    			}
	    			if(x>=arr[end])
    				{

    					out.println((end+1)+" "+(arr[end]-((end==0)?0:arr[end-1])));
    					continue;
    				}
	    			if(x<=arr[start])
    				{

    					out.println((1)+" "+x);
    					continue;
    				}
	    			while(start<=end)
	    			{
	    				int mid=start+(end-start)/2;
	    				if(x==arr[mid])
	    				{
	    					out.println((mid+1)+" "+(arr[mid]));
	    					break;
	    				}
	    				if(mid+1<n)
	    				{
	    				if(x>=arr[mid] && x<arr[mid+1])
	    				{
	    					out.println((mid+2)+" "+(x-arr[mid]));
	    					break;
	    				}
	    				}
	    				if(x<arr[mid])
	    				{
	    					end=mid-1;
	    				}
	    				else if(x>arr[mid] && x>=arr[mid+1])
	    				{
	    					start=mid+1;
	    				}
	    			}
	    		}

	    }

	    static long power(long x, long y, long p)
	    {

	        long res = 1;

	        x = x % p;

	       if (x == 0) return 0;

	        while (y > 0)
	        {
	            if((y & 1)==1)
	                res = (res * x) % p;
	            y = y >> 1;
	            x = (x * x) % p;
	        }
	        return res;
	    }

	    static Throwable uncaught;

	    BufferedReader in;
	    FastScanner sc;
	    PrintWriter out;

	    @Override
	    public void run() {
	        try {
	            in = new BufferedReader(new InputStreamReader(System.in));
	            out = new PrintWriter(System.out);
	            sc = new FastScanner(in);
	            solve();
	        } catch (Throwable uncaught) {
	            Solution.uncaught = uncaught;
	        } finally {
	            out.close();
	        }
	    }

	    public static void main(String[] args) throws Throwable {
	        Thread thread = new Thread(null, new Solution(), "", (1 << 26));
	        thread.start();
	        thread.join();
	        if (Solution.uncaught != null) {
	            throw Solution.uncaught;
	        }
	    }

	}

	class FastScanner {

	    BufferedReader in;
	    StringTokenizer st;

	    public FastScanner(BufferedReader in) {
	        this.in = in;
	    }

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

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

	    public int[] readArray(int n) throws Exception {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

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

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

	}


	import java.util.*;

import java.io.*;
	import java.math.*;
	import java.awt.geom.*;

	import static java.lang.Math.*;

	public class Solution implements Runnable {

	    public void solve() throws Exception {

//	    	int t=sc.nextInt();
//	    	long mod=(long)1e9+7;
//
//	    	while(t-->0)
//	    	{
	    		int n=sc.nextInt();
	    		int m=sc.nextInt();
	    		long arr[]=new long[n];

	    		for(int i=0;i<n;i++)
	    		{
	    			arr[i]=sc.nextLong();
	    		}

	    		for(int i=1;i<n;i++)
	    		{

	    			arr[i]=arr[i]+arr[i-1];
	    			//out.println(arr[i]);
	    		}
	    		for(int i=0;i<m;i++)
	    		{
	    			long x=sc.nextLong();
	    			int start=0;
	    			int end=n-1;
	    			if(start==end)
	    			{
	    				out.println(1+" "+x);
	    				continue;
	    			}
	    			if(x>=arr[end])
    				{

    					out.println((end+1)+" "+(arr[end]-((end==0)?0:arr[end-1])));
    					continue;
    				}
	    			if(x<=arr[start])
    				{

    					out.println((1)+" "+x);
    					continue;
    				}
	    			while(start<=end)
	    			{
	    				int mid=start+(end-start)/2;
	    				if(x==arr[mid])
	    				{
	    					out.println((mid+1)+" "+(arr[mid]));
	    					break;
	    				}
	    				if(mid+1<n)
	    				{
	    				if(x>=arr[mid] && x<arr[mid+1])
	    				{
	    					out.println((mid+2)+" "+(x-arr[mid]));
	    					break;
	    				}
	    				}
	    				if(x<arr[mid])
	    				{
	    					end=mid-1;
	    				}
	    				else if(x>arr[mid] && x>arr[mid+1])
	    				{
	    					start=mid+1;
	    				}
	    			}
	    		}

	    }

	    static long power(long x, long y, long p)
	    {

	        long res = 1;

	        x = x % p;

	       if (x == 0) return 0;

	        while (y > 0)
	        {
	            if((y & 1)==1)
	                res = (res * x) % p;
	            y = y >> 1;
	            x = (x * x) % p;
	        }
	        return res;
	    }

	    static Throwable uncaught;

	    BufferedReader in;
	    FastScanner sc;
	    PrintWriter out;

	    @Override
	    public void run() {
	        try {
	            in = new BufferedReader(new InputStreamReader(System.in));
	            out = new PrintWriter(System.out);
	            sc = new FastScanner(in);
	            solve();
	        } catch (Throwable uncaught) {
	            Solution.uncaught = uncaught;
	        } finally {
	            out.close();
	        }
	    }

	    public static void main(String[] args) throws Throwable {
	        Thread thread = new Thread(null, new Solution(), "", (1 << 26));
	        thread.start();
	        thread.join();
	        if (Solution.uncaught != null) {
	            throw Solution.uncaught;
	        }
	    }

	}

	class FastScanner {

	    BufferedReader in;
	    StringTokenizer st;

	    public FastScanner(BufferedReader in) {
	        this.in = in;
	    }

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

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

	    public int[] readArray(int n) throws Exception {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}

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

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

	}


import java.util.Scanner;

public class letters {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        long a[] = new long[n];
        long b[] = new long[m];

            for (int i = 0; i < a.length; i++)
            {
                a[i] = sc.nextLong();
            }
            for (int i = 0; i < b.length; i++)
            {
                b[i] = sc.nextLong();
            }
        int j=0;
        for (int i = 0; i < b.length;)
        {
            if(a[j]>=b[i])
            {
                System.out.println((j+1)+" "+b[i]);
                i++;
            }
            else
            {
                for (int k = i; k < b.length; k++) {
                    b[k] = b[k] - a[j];
                }
                j++;
            }
        }
    }
}


import java.util.Scanner;

public class letters {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a[] = new int[n];
        int b[] = new int[m];
            for (int i = 0; i < a.length; i++)
            {
                a[i] = sc.nextInt();
            }
            for (int i = 0; i < b.length; i++)
            {
                b[i] = sc.nextInt();
            }
        int j=0;
        for (int i = 0; i < b.length;)
        {
            if(a[j]>=b[i])
            {
                System.out.println((j+1)+" "+b[i]);
                i++;
            }
            else
            {
                for (int k = i; k < b.length; k++) {
                    b[k] = b[k] - a[j];
                }
                j++;
            }
        }
    }
}


import java.util.Scanner;

public class letters {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        long a[] = new long[n];
        long b[] = new long[m];

            for (int i = 0; i < a.length; i++)
            {
                a[i] = sc.nextLong();
            }
            for (int i = 0; i < b.length; i++)
            {
                b[i] = sc.nextLong();
            }
        int j=0;
        for (int i = 0; i < b.length;)
        {
            if(a[j]>=b[i])
            {
                if(j>0)
                System.out.println((j+1)+" "+(b[i]-a[j-1]));
                else
                System.out.println((j+1)+" "+b[i]);

                i++;
            }
            else
            {
                // for (int k = i; k < b.length; k++) {
                //     b[k] = b[k] - a[j];
                //}

                j++;
                a[j] = a[j]+a[j-1];
            }
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

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

    static class CLetters {
        long[] prefS;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            long[] arr = in.nextLongArray(n + 1, 1);
            prefS = new long[n + 1];
            for (int i = 1; i < n + 1; i++) {
                prefS[i] = prefS[i - 1] + arr[i];
            }
//        debug(prefS);
            while (m-- > 0) {
                long k = in.nextLong();
                _C.Pair<Integer, Long> ans = search(k, n);
                out.println(ans.f + 1, ans.s);
            }
            out.flush();
        }

        _C.Pair<Integer, Long> search(long k, int n) {
            int l = 0;
            int r = n;
            while (l <= r) {
                int mid = l + (r - l + 1) / 2;
                if (mid <= n && prefS[mid] < k) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
            return new _C.Pair(r, k - prefS[r]);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; ++i) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            writer.flush();
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public long[] nextLongArray(int n, int startFrom) {
            long[] array = new long[n];
            for (int i = startFrom; i < n; ++i) array[i] = nextLong();
            return array;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class _C {
        static public class Pair<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<_C.Pair<F, S>> {
            public F f;
            public S s;

            public Pair(F f, S s) {
                this.f = f;
                this.s = s;
            }

            public int compareTo(_C.Pair<F, S> o) {
                int t = f.compareTo(o.f);
                if (t == 0) return s.compareTo(o.s);
                return t;
            }

            public int hashCode() {
                return (31 + f.hashCode()) * 31 + s.hashCode();
            }

            public boolean equals(Object o) {
                if (!(o instanceof _C.Pair)) return false;
                if (o == this) return true;
                _C.Pair p = (_C.Pair) o;
                return f.equals(p.f) && s.equals(p.s);
            }

            public String toString() {
                return "{" + f + ", " + s + "}";
            }

        }

    }
}


//package pkg978c;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        long n=sc.nextInt();
        long m=sc.nextInt();
        long room[]=new long[(int)n];
        room[0]=sc.nextInt();
        for(long i=1;i<n;i++)
        {
            room[(int)i]=room[(int)i-1]+sc.nextLong();
        }

        for(int j=0;j<m;j++)
        {
            int i=1;
            long num=sc.nextLong();
//            while(room[i]<num && i<n)
//            {
//                i++;
//            }
//
//            if(i>0)
//                System.out.println((i+1)+" "+(num-room[i-1]));
//            else
//            {
//                System.out.println((i+1)+" "+(num));
//            }
            for(;room[i]<num;i++)
            {

            }
            if(num>room[0])
               System.out.println((i)+" "+(num-room[i-1]));
            else
            {
                System.out.println((i)+" "+(num));
            }
            //System.out.println(i +" "+(num-room[i-1]));

        }
    }

}


//package pkg978c;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        long n=sc.nextInt();
        long m=sc.nextInt();
        long room[]=new long[(int)n];
        //int letter[]=new int[m];
        room[0]=sc.nextInt();
        for(long i=1;i<n;i++)
        {
            room[(int)i]=room[(int)i-1]+sc.nextLong();
        }
        for(int j=0;j<m;j++)
        {
            int i=0;
            long num=sc.nextLong();
            while(room[i]<num && i<n)
            {
                i++;
            }
           // System.out.println("i-1:"+(i-1));
            if(i>0)
                System.out.println((i+1)+" "+(num-room[i-1]));
            else
            {
                System.out.println((i+1)+" "+(num));
            }

        }
    }

}


//package pkg978c;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        long n=sc.nextInt();
        long m=sc.nextInt();
        long room[]=new long[(int)n+1];
        room[0]=0;
        for(long i=1;i<=n;i++)
        {
            room[(int)i]=room[(int)i-1]+sc.nextLong();
        }
        int i=1;
        for(int j=0;j<m;j++)
        {

            long num=sc.nextLong();
//            while(room[i]<num && i<n)
//            {
//                i++;
//            }
//
//            if(i>0)
//                System.out.println((i+1)+" "+(num-room[i-1]));
//            else
//            {
//                System.out.println((i+1)+" "+(num));
//            }
           for(;room[i]<num;i++)
            {

            }
//            if(num>room[0])
//               System.out.println((i+1)+" "+(num-room[i-1]));
//            else
//            {
//                System.out.println((i)+" "+(num));
//            }
              System.out.println(i +" "+(num-room[i-1]));

        }
    }

}


//package pkg978c;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        long n=sc.nextInt();
        long m=sc.nextInt();
        long room[]=new long[(int)n];
        room[0]=sc.nextInt();
        for(long i=1;i<n;i++)
        {
            room[(int)i]=room[(int)i-1]+sc.nextLong();
        }

        for(int j=0;j<m;j++)
        {
            int i=1;
            long num=sc.nextLong();
//            while(room[i]<num && i<n)
//            {
//                i++;
//            }
//
//            if(i>0)
//                System.out.println((i+1)+" "+(num-room[i-1]));
//            else
//            {
//                System.out.println((i+1)+" "+(num));
//            }
            for(;room[i]<num;i++)
            {

            }
            if(num>room[0])
               System.out.println((i+1)+" "+(num-room[i-1]));
            else
            {
                System.out.println((i)+" "+(num));
            }
            //System.out.println(i +" "+(num-room[i-1]));

        }
    }

}


import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner Scan = new Scanner(System.in);
        int n= Scan.nextInt(),m= Scan.nextInt(),x,y;
        int[] a = new int[n],b = new int[m];
        for (int i = 1; i <=n ; i++) {
            a[i-1]=Scan.nextInt();
        }
        for (int i = 0; i <m ; i++) {
            b[i]=Scan.nextInt();
        }
        y = a[0];
        for (int i = 0,j =1; i <m ; i++) {
            if(b[i]>y) {
                j++;
                y += a[j-1];
            }
            x = y - b[i];
            System.out.println(j+" "+(a[j-1]-x));
        }
    }
}


import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner Scan = new Scanner(System.in);
        int n= Scan.nextInt(),m= Scan.nextInt();
        long x,y;
        long[] a = new long[n],b = new long[m];
        for (int i = 1; i <=n ; i++) {
            a[(int)i-1]=Scan.nextLong();
        }
        for (int i = 0; i <m ; i++) {
            b[i]=Scan.nextLong();
        }
        y = a[0];
        for (int i = 0,j =1; i <m ; i++) {
            if(b[i]>y) {
                j++;
                y += a[j-1];
            }
            x =Math.abs( y - b[i]);
            System.out.println(j+" "+(a[j-1]-x));
        }
    }
}


import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner Scan = new Scanner(System.in);
        long n= Scan.nextLong(),m= Scan.nextLong(),x,y;
        int[] a = new int[n],b = new int[m];
        for (long i = 1; i <=n ; i++) {
            a[i-1]=Scan.nextInt();
        }
        for (long i = 0; i <m ; i++) {
            b[i]=Scan.nextInt();
        }
        y = a[0];
        for (long i = 0,j =1; i <m ; i++) {
            if(b[i]>y) {
                j++;
                y += a[j-1];
            }
            x = y - b[i];
            System.out.println(j+" "+(a[j-1]-x));
        }
    }
}


import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner Scan = new Scanner(System.in);
        int n= Scan.nextInt(),m= Scan.nextInt();
        long x,y;
        long[] a = new long[n],b = new long[m];
        for (int i = 1; i <=n ; i++) {
            a[(int)i-1]=Scan.nextLong();
        }
        for (int i = 0; i <m ; i++) {
            b[i]=Scan.nextLong();
        }
        y = a[0];
        for (int i = 0,j =1; i <m ; i++) {
            if(b[i]>y) {
                j++;
                y += a[j-1];
            }
            x = y - b[i];
            System.out.println(j+" "+(a[j-1]-x));
        }
    }
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<sstream>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;

int a[4010];
int dp[4010][4010];
vector<int> e[200010];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		e[a[i]].push_back(i);
		dp[0][i]=1;
	}
	int ans=1;
	for(int i=2;i<=n;i++){
		for(int j=1;j<i;j++){
			int t=a[i];
			int left=0,right=e[t].size()-1,mid,p=-1;
			while(left<=right){
				mid=(left+right)>>1;
				if(e[t][mid]<j){
					left=mid+1;
					p=mid;
				}else
					right=mid-1;
			}
			if(p!=-1){
				dp[i][j]=dp[j][p]+1;
			}else
				dp[i][j]=2;
			ans=max(ans,dp[i][j]);
		}
	}
	printf("%d\n",ans);
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<sstream>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;

int a[4010];
int dp[4010][4010];
vector<int> e[1000010];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		e[a[i]].push_back(i);
		dp[0][i]=1;
	}
	int ans=1;
	for(int i=2;i<=n;i++){
		for(int j=1;j<i;j++){
			int t=a[i];
			int left=0,right=e[t].size()-1,mid,p=-1;
			while(left<=right){
				mid=(left+right)>>1;
				if(e[t][mid]<j){
					left=mid+1;
					p=e[t][mid];
				}else
					right=mid-1;
			}
			if(p!=-1){
				dp[i][j]=dp[j][p]+1;
			}else
				dp[i][j]=2;
			ans=max(ans,dp[i][j]);
		}
	}
	printf("%d\n",ans);
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<sstream>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;

int a[4010];
int dp[4010][4010];
vector<int> e[200010];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		e[a[i]].push_back(i);
		dp[0][i]=1;
	}
	int ans=1;
	for(int i=2;i<=n;i++){
		for(int j=1;j<i;j++){
			int t=a[i];
			int left=0,right=e[t].size()-1,mid,p=-1;
			while(left<=right){
				mid=(left+right)>>1;
				if(e[t][mid]<j){
					left=mid+1;
					p=e[t][mid];
				}else
					right=mid-1;
			}
			if(p!=-1){
				dp[i][j]=dp[j][p]+1;
			}else
				dp[i][j]=2;
			ans=max(ans,dp[i][j]);
		}
	}
	printf("%d\n",ans);
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<sstream>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;

int a[4010];
int dp[4010][4010];
vector<int> e[100010];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		e[a[i]].push_back(i);
		dp[0][i]=1;
	}
	int ans=1;
	for(int i=2;i<=n;i++){
		for(int j=1;j<i;j++){
			int t=a[j]*2-a[i];
			int left=0,right=e[t].size()-1,mid,p=-1;
			while(left<=right){
				mid=(left+right)>>1;
				if(e[t][mid]<j){
					left=mid+1;
					p=mid;
				}else
					right=mid-1;
			}
			if(p!=-1){
				dp[i][j]=dp[j][p]+1;
			}else
				dp[i][j]=2;
			ans=max(ans,dp[i][j]);
		}
	}
	printf("%d\n",ans);
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<iostream>
#include<assert.h>
using namespace std;
#define MAXN 4006
int array[MAXN];
vector < set < int > > S(MAXN);
int comprs(int N)
{
    map < int , int > M;
    map < int , int > :: iterator im;
    int i;
    for (i=1; i<=N; ++i) M[array[i]] = -1;
    for (im = M.begin(), i = 1; im != M.end(); ++im, ++i) im->second = i;
    for (i=1; i<=N; ++i) array[i] = M[array[i]];
    return (--im)->second;
}
int find(set < int > &S1, set < int > &S2)
{
    set < int > :: iterator is1, is2;
    //printf("xxx  "); for (is1 = S1.begin(); is1 != S1.end(); ++is1) printf("%d ", *is1); puts("");
    //printf("xxx  "); for (is2 = S2.begin(); is2 != S2.end(); ++is2) printf("%d ", *is2); puts("");
    is1 = S1.begin(), is2 = S2.begin();
    int ans = 1, last, now;
    now = 2, last = *is1++;
    while (is1 != S1.end() || is2 != S2.end())
    {
        if (now == 1)
        {
            is1 = S1.upper_bound(last);
            if (is1 == S1.end()) break;
            last = *is1;
            ++ans;
            now = 2;
        }
        else
        {
            is2 = S2.upper_bound(last);
            if (is2 == S2.end()) break;
            last = *is2;
            ++ans;
            now = 1;
        }
    }
    return ans;
}
int main()
{
    int N,n,i,j,ans,temp;
    while (scanf("%d", &N) != EOF)
    {
        for (i=1; i<=N; ++i)
        {
            scanf("%d", &array[i]);
            S[i].clear();
        }
        n = comprs(N);
        for (i=1; i<=N; ++i) S[array[i]].insert(i);
        N = n;
        ans = 1;
        for (i=1; i<=N; ++i)
        {
            for (j=i+1; j<=N; ++j)
            {
                temp = find(S[i],S[j]);
                if (temp > ans) ans = temp;
                temp = find(S[h],S[i]);
                if (temp > ans) ans = temp;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<iostream>
#include<assert.h>
using namespace std;
#define MAXN 4006
int array[MAXN];
vector < set < int > > S(MAXN);
int comprs(int N)
{
    map < int , int > M;
    map < int , int > :: iterator im;
    int i;
    for (i=1; i<=N; ++i) M[array[i]] = -1;
    for (im = M.begin(), i = 1; im != M.end(); ++im, ++i) im->second = i;
    for (i=1; i<=N; ++i) array[i] = M[array[i]];
    return (--im)->second;
}
int find(set < int > &S1, set < int > &S2)
{
    set < int > :: iterator is1, is2;
    //printf("xxx  "); for (is1 = S1.begin(); is1 != S1.end(); ++is1) printf("%d ", *is1); puts("");
    //printf("xxx  "); for (is2 = S2.begin(); is2 != S2.end(); ++is2) printf("%d ", *is2); puts("");
    is1 = S1.begin(), is2 = S2.begin();
    int ans = 1, last, now;
    now = 2, last = *is1++;
    while (is1 != S1.end() || is2 != S2.end())
    {
        if (now == 1)
        {
            is1 = S1.upper_bound(last);
            if (is1 == S1.end()) break;
            last = *is1;
            ++ans;
            now = 2;
        }
        else
        {
            is2 = S2.upper_bound(last);
            if (is2 == S2.end()) break;
            last = *is2;
            ++ans;
            now = 1;
        }
    }
    return ans;
}
int main()
{
    int N,n,i,j,ans,temp;
    while (scanf("%d", &N) != EOF)
    {
        for (i=1; i<=N; ++i)
        {
            scanf("%d", &array[i]);
            S[i].clear();
        }
        n = comprs(N);
        for (i=1; i<=N; ++i) S[array[i]].insert(i);
        N = n;
        ans = 1;
        for (i=1; i<=N; ++i)
        {
            for (j=i+1; j<=N; ++j)
            {
                temp = find(S[i],S[j]);
                if (temp > ans) ans = temp;
                temp = find(S[j],S[i]);
                if (temp > ans) ans = temp;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<iostream>
#include<assert.h>
using namespace std;
#define MAXN 4006
int array[MAXN];
vector < set < int > > S(MAXN);
int comprs(int N)
{
    map < int , int > M;
    map < int , int > :: iterator im;
    int i;
    for (i=1; i<=N; ++i) M[array[i]] = -1;
    for (im = M.begin(), i = 1; im != M.end(); ++im, ++i) im->second = i;
    for (i=1; i<=N; ++i) array[i] = M[array[i]];
    return (--im)->second;
}
int find(set < int > &S1, set < int > &S2)
{
    set < int > :: iterator is1, is2;
    //printf("xxx  "); for (is1 = S1.begin(); is1 != S1.end(); ++is1) printf("%d ", *is1); puts("");
    //printf("xxx  "); for (is2 = S2.begin(); is2 != S2.end(); ++is2) printf("%d ", *is2); puts("");
    is1 = S1.begin(), is2 = S2.begin();
    int ans = 1, last, now;
    now = 2, last = *is1++;
    while (is1 != S1.end() || is2 != S2.end())
    {
        if (now == 1)
        {
            is1 = S1.upper_bound(last);
            if (is1 == S1.end()) break;
            last = *is1;
            ++ans;
            now = 2;
        }
        else
        {
            is2 = S2.upper_bound(last);
            if (is2 == S2.end()) break;
            last = *is2;
            ++ans;
            now = 1;
        }
    }
    return ans;
}
int main()
{
    int N,n,i,j,ans,temp;
    while (scanf("%d", &N) != EOF)
    {
        for (i=1; i<=N; ++i)
        {
            scanf("%d", &array[i]);
            S[i].clear();
        }
        n = comprs(N);
        for (i=1; i<=N; ++i) S[array[i]].insert(i);
        N = n;
        ans = 1;
        for (i=1; i<=N; ++i)
        {
            temp = S[i].size();
            if (temp > ans) ans = temp;
            for (j=i+1; j<=N; ++j)
            {
                temp = find(S[i],S[j]);
                if (temp > ans) ans = temp;
                temp = find(S[j],S[i]);
                if (temp > ans) ans = temp;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<map>
#include<utility>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<iostream>
#include<assert.h>
using namespace std;
#define MAXN 4006
int array[MAXN];
vector < set < int > > S(MAXN);
int comprs(int N)
{
    map < int , int > M;
    map < int , int > :: iterator im;
    int i;
    for (i=1; i<=N; ++i) M[array[i]] = -1;
    for (im = M.begin(), i = 1; im != M.end(); ++im, ++i) im->second = i;
    for (i=1; i<=N; ++i) array[i] = M[array[i]];
    return (--im)->second;
}
int find(set < int > &S1, set < int > &S2)
{
    set < int > :: iterator is1, is2;
    //printf("xxx  "); for (is1 = S1.begin(); is1 != S1.end(); ++is1) printf("%d ", *is1); puts("");
    //printf("xxx  "); for (is2 = S2.begin(); is2 != S2.end(); ++is2) printf("%d ", *is2); puts("");
    is1 = S1.begin(), is2 = S2.begin();
    int ans = 1, last, now;
    if (*is1 < *is2) now = 2, last = *is1++;
    else now = 1, last = *is2++;
    while (is1 != S1.end() || is2 != S2.end())
    {
        if (now == 1)
        {
            is1 = S1.upper_bound(last);
            if (is1 == S1.end()) break;
            last = *is1;
            ++ans;
            now = 2;
        }
        else
        {
            is2 = S2.upper_bound(last);
            if (is2 == S2.end()) break;
            last = *is2;
            ++ans;
            now = 1;
        }
    }
    return ans;
}
int main()
{
    int N,n,i,j,ans,temp;
    while (scanf("%d", &N) != EOF)
    {
        for (i=1; i<=N; ++i)
        {
            scanf("%d", &array[i]);
            S[i].clear();
        }
        n = comprs(N);
        for (i=1; i<=N; ++i) S[array[i]].insert(i);
        N = n;
        ans = 1;
        for (i=1; i<=N; ++i)
        {
            for (j=i+1; j<=N; ++j)
            {
                temp = find(S[i],S[j]);
                if (temp > ans) ans = temp;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];
int q[maxn * 2], r, q1[maxn * 2], r1;

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < (int)nxt[orig[j]].size() - 1; i++) for(int t = i + 1; t < (int) nxt[orig[j]].size(); t++) f[nxt[orig[j]][i]][nxt[orig[j]][t]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		r = 0; r1 = 0;
		q1[r1++] = i; q1[r1++] = j;
		for(;heada != nxt[orig[i]].end();){
			q[r++] = *heada;
			if(headb != nxt[orig[j]].end()) q[r++] = *headb;
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), q[r - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < r) ans = r;
		for(int i = 0; i < r - 1; i++) for(int j = i + 1; j < r; j += 2) f[q[i]][q[j]] = 1;
/*		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].begin(), nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end(), q1[i + 1]);
			if(head != nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end()) q1[r1++] = *head;
			else break;
		}
		if(ans < r1) ans = r1;
		for(int i = 0; i < r1 - 1; i++) for(int j = i + 1; j < r; j++) f[q1[i]][q1[j]] = 1;*/
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
map <pii, bool> mp;
vi nxt[1000100];
bool f1[1000100];
int q[maxn * 2], r, q1[maxn * 2], r1;

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(orig[i] == orig[j]) continue;
		if(mp[make_pair(orig[i], orig[j])]) continue;
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		mp[make_pair(orig[i], orig[j])] = 1;
		mp[make_pair(orig[j], orig[i])] = 1;
		r = 0; r1 = 0;
		q1[r1++] = i; q1[r1++] = j;
		for(;heada != nxt[orig[i]].end();){
			q[r++] = *heada;
			if(headb != nxt[orig[j]].end()) q[r++] = *headb;
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), q[r - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < r) ans = r;
/*		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].begin(), nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end(), q1[i + 1]);
			if(head != nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end()) q1[r1++] = *head;
			else break;
		}
		if(ans < r1) ans = r1;
		for(int i = 0; i < r1 - 1; i++) for(int j = i + 1; j < r; j++) f[q1[i]][q1[j]] = 1;*/
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];
int q[maxn * 2], r, q1[maxn * 2], r1;

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < (int)nxt[orig[j]].size() - 1; i++) f[nxt[orig[j]][i]][nxt[orig[j]][i + 1]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		r = 0; r1 = 0;
		q1[r1++] = i; q1[r1++] = j;
		for(;heada != nxt[orig[i]].end();){
			q[r++] = *heada;
			if(headb != nxt[orig[j]].end()) q[r++] = *headb;
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), q[r - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < r) ans = r;
		for(int i = 0; i < r - 1; i++) f[q[i]][q[i + 1]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].begin(), nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end(), q1[i + 1]);
			if(head != nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end()) q1[r1++] = *head;
			else break;
		}
		if(ans < r1) ans = r1;
		for(int i = 0; i < r1 - 1; i++) f[q1[i]][q1[i + 1]] = 1;
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
map <pii, bool> mp;
vi nxt[1000100];
int f1[1000100];
bool f[maxn][maxn];
int q[maxn * 2], r, q1[maxn * 2], r1;

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
		}
	}
	for(int i = 0, k = 1; i < 1000001; i++)if(f1[i]) f1[i] = k++;
	for(int i = 0; i < n; i++)
		for(int j = i + 1; j < n; j++){
		if(orig[i] == orig[j]) continue;
		if(f[f1[orig[i]]][f1[orig[j]]]) continue;
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		f[f1[orig[i]]][f1[orig[j]]] = 1;
		f[f1[orig[j]]][f1[orig[i]]] = 1;
		r = 0; r1 = 0;
		q1[r1++] = i; q1[r1++] = j;
		for(;heada != nxt[orig[i]].end();){
			q[r++] = *heada;
			if(headb != nxt[orig[j]].end()) q[r++] = *headb;
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), q[r - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < r) ans = r;
/*		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].begin(), nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end(), q1[i + 1]);
			if(head != nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end()) q1[r1++] = *head;
			else break;
		}
		if(ans < r1) ans = r1;
		for(int i = 0; i < r1 - 1; i++) for(int j = i + 1; j < r; j++) f[q1[i]][q1[j]] = 1;*/
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];
vi v, v1;

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < nxt[orig[j]].size() - 1; i++) f[nxt[orig[j]][i]][nxt[orig[j]][i + 1]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		while(v1.size()) v1.erase(v1.end() - 1);
		while(v.size()) v.erase(v.end() - 1);
		v1.push_back(i); v1.push_back(j);
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		for(;heada != nxt[orig[i]].end();){
			v.push_back(*heada);
			if(headb != nxt[orig[j]].end()) v.push_back(*headb);
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), v[v.size() - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < v.size()) ans = v.size();
		for(int i = 0; i < v.size() - 1; i++) f[v[i]][v[i + 1]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].begin(), nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end(), v1[i + 1]);
			if(head != nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end()) v1.push_back(*head);
			else break;
		}
		if(ans < v1.size()) ans = v1.size();
		for(int i = 0; i < v1.size() - 1; i++) f[v1[i]][v1[i + 1]] = 1;
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4000;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < nxt[orig[j]].size() - 1; i++) f[nxt[orig[j]][i]][nxt[orig[j]][i + 1]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi v, v1;
		v1.push_back(i); v1.push_back(j);
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		for(;heada != nxt[orig[i]].end();){
			v.push_back(*heada);
			if(headb != nxt[orig[j]].end()) v.push_back(*headb);
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), v[v.size() - 1]);
			int k = *heada;
			headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), k);
		}
		if(ans < v.size()) ans = v.size();
		for(int i = 0; i < v.size() - 1; i++) f[v[i]][v[i + 1]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].begin(), nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end(), v1[i + 1]);
			if(head != nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end()) v1.push_back(*head);
			else break;
		}
		if(ans < v1.size()) ans = v1.size();
		for(int i = 0; i < v.size() - 1; i++) f[v1[i]][v1[i + 1]] = 1;
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < nxt[orig[j]].size() - 1; i++) f[nxt[orig[j]][i]][nxt[orig[j]][i + 1]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi v, v1;
		v1.push_back(i); v1.push_back(j);
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		for(;heada != nxt[orig[i]].end();){
			v.push_back(*heada);
			if(headb != nxt[orig[j]].end()) v.push_back(*headb);
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), v[v.size() - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < v.size()) ans = v.size();
		for(int i = 0; i < v.size() - 1; i++) f[v[i]][v[i + 1]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].begin(), nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end(), v1[i + 1]);
			if(head != nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end()) v1.push_back(*head);
			else break;
		}
		if(ans < v1.size()) ans = v1.size();
		for(int i = 0; i < v1.size() - 1; i++) f[v1[i]][v1[i + 1]] = 1;
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];
int q[maxn * 2], r, q1[maxn * 2], r1;

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < (int)nxt[orig[j]].size() - 1; i++) for(int t = i + 1; t < (int) nxt[orig[j]].size(); t++) f[nxt[orig[j]][i]][nxt[orig[j]][t]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		r = 0; r1 = 0;
		q1[r1++] = i; q1[r1++] = j;
		for(;heada != nxt[orig[i]].end();){
			q[r++] = *heada;
			if(headb != nxt[orig[j]].end()) q[r++] = *headb;
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), q[r - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < r) ans = r;
		for(int i = 0; i < r - 1; i++) for(int j = i + 1; j < r; j += 2) f[q[i]][q[j]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].begin(), nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end(), q1[i + 1]);
			if(head != nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end()) q1[r1++] = *head;
			else break;
		}
		if(ans < r1) ans = r1;
		for(int i = 0; i < r1 - 1; i++) for(int j = i + 1; j < r; j++) f[q1[i]][q1[j]] = 1;
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];

int main(){
	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < nxt[orig[j]].size() - 1; i++) f[nxt[orig[j]][i]][nxt[orig[j]][i + 1]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi v, v1;
		v1.push_back(i); v1.push_back(j);
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		for(;heada != nxt[orig[i]].end();){
			v.push_back(*heada);
			if(headb != nxt[orig[j]].end()) v.push_back(*headb);
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), v[v.size() - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < v.size()) ans = v.size();
		for(int i = 0; i < v.size() - 1; i++) f[v[i]][v[i + 1]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].begin(), nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end(), v1[i + 1]);
			if(head != nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end()) v1.push_back(*head);
			else break;
		}
		if(ans < v1.size()) ans = v1.size();
		for(int i = 0; i < v1.size() - 1; i++) f[v1[i]][v1[i + 1]] = 1;
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];
int q[maxn * 2], r, q1[maxn * 2], r1;

int main(){
	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < (int)nxt[orig[j]].size() - 1; i++) f[nxt[orig[j]][i]][nxt[orig[j]][i + 1]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		r = 0; r1 = 0;
		q1[r1++] = i; q1[r1++] = j;
		for(;heada != nxt[orig[i]].end();){
			q[r++] = *heada;
			if(headb != nxt[orig[j]].end()) q[r++] = *headb;
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), q[r - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < r) ans = r;
		for(int i = 0; i < r - 1; i++) f[q[i]][q[i + 1]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].begin(), nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end(), q1[i + 1]);
			if(head != nxt[orig[q1[i + 1]] * 2 - orig[q1[i]]].end()) q1[r1++] = *head;
			else break;
		}
		if(ans < r1) ans = r1;
		for(int i = 0; i < r1 - 1; i++) f[q1[i]][q1[i + 1]] = 1;
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b) {return a > b ? a : b;}
template <class T> inline T min1(T a, T b) {return a < b ? a : b;}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}
const int maxn = 4100;
int n;
int orig[maxn];
bool f[maxn][maxn];
vi nxt[1000100];
bool f1[1000100];

int main(){
//	freopen("input.txt","r", stdin);
//	ifstream cin("input.txt");
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		nxt[orig[i]].push_back(i);
	}
	int ans = 0;

	for(int j = 0; j < n; j++){
		if(f1[orig[j]] == 0){
			f1[orig[j]] = 1;
			ans = max1(ans, (int)nxt[orig[j]].size());
			for(int i = 0; i < nxt[orig[j]].size() - 1; i++) f[nxt[orig[j]][i]][nxt[orig[j]][i + 1]] = 1;
		}
	}
	for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++){
		if(f[i][j]) continue;
		vi v, v1;
		v1.push_back(i); v1.push_back(j);
		vi :: iterator heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), i);
		vi :: iterator headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), j);
		for(;heada != nxt[orig[i]].end();){
			v.push_back(*heada);
			if(headb != nxt[orig[j]].end()) v.push_back(*headb);
			else break;
			heada = lower_bound(nxt[orig[i]].begin(), nxt[orig[i]].end(), v[v.size() - 1]);
			if(heada != nxt[orig[i]].end()) headb = lower_bound(nxt[orig[j]].begin(), nxt[orig[j]].end(), *heada);
		}
		if(ans < v.size()) ans = v.size();
		for(int i = 0; i < v.size() - 1; i++) f[v[i]][v[i + 1]] = 1;
		for(int i = 0; ; i++){
			vi :: iterator head = lower_bound(nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].begin(), nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end(), v1[i + 1]);
			if(head != nxt[orig[v1[i + 1]] * 2 - orig[v1[i]]].end()) v1.push_back(*head);
			else break;
		}
		if(ans < v1.size()) ans = v1.size();
		for(int i = 0; i < v.size() - 1; i++) f[v1[i]][v1[i + 1]] = 1;
	}
	cout << ans << endl;

	return 0;
}


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

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define debug(x) cout<<#x<<"="<<x<<endl;

const int MAX_NUM = 100010;

bool t_vis[MAX_NUM],flag[MAX_NUM];
int cnt[MAX_NUM];
bool tt[MAX_NUM];
int b[4444];

int main()
{
    int n;
    cin>>n;rep(i,n)cin>>b[i];

    int ans = 0;
    rep(i,n) cnt[ b[i] ] ++;
    rep(i,n) {
        ans = max(ans , cnt[ b[i] ]);
        cnt[ b[i] ] = 0;
    }
    rep(i,n) if(!flag[ b[i] ]) {
        flag[ b[i] ] = 1;
        int num = 0, las = i;
        for(int j=i;j<n;++j) {
            if( b[j] == b[i] ) {
                num ++;
                for(int k=las;k<j;++k) if(t_vis[ b[k] ]){
                    cnt[ b[k] ] ++;
                    t_vis[ b[k] ] = 0;
                }
                las = j+1;
                continue;
            }
            t_vis[ b[j] ] ++;
        }
        for(int k=i;k<las;++k) if( b[k] != b[i] && !tt[ b[k] ]) {
            cnt[ b[k] ] = cnt[ b[k] ] + cnt[ b[k] ] + 1;
            tt[ b[k] ] = 1;
        }
        for(int k=las;k<n;++k) if(t_vis[ b[k] ]){
            cnt[ b[k] ] ++;
            t_vis[ b[k] ] = 0;
        }
        for(int j=i;j<n;++j) {
            ans = max( ans , cnt[ b[j] ] );
            cnt[ b[j] ] = 0;
            tt[ b[j] ] = 0;
        }
    }
    cout<<ans<<endl;

    return 0;
}


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

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define debug(x) cout<<#x<<"="<<x<<endl;

const int MAX_NUM = 100010;

bool t_vis[MAX_NUM],flag[MAX_NUM];
int cnt[MAX_NUM];

int b[4444];

int main()
{
    int n;
    cin>>n;rep(i,n)cin>>b[i];

    int ans = 0;
    rep(i,n) cnt[ b[i] ] ++;
    rep(i,n) {
        ans = max(ans , cnt[ b[i] ]);
        cnt[ b[i] ] = 0;
    }
    rep(i,n) if(!flag[ b[i] ]) {
        flag[ b[i] ] = 1;
        int num = 0, las = i;
        for(int j=i;j<n;++j) {
            if( b[j] == b[i] ) {
                num ++;
                for(int k=las;k<j;++k) if(t_vis[ b[k] ]){
                    cnt[ b[k] ] ++;
                    t_vis[ b[k] ] = 0;
                }
                las = j+1;
                continue;
            }
            t_vis[ b[j] ] ++;
        }
        for(int k=las;k<n;++k) if(t_vis[ b[k] ]){
            cnt[ b[k] ] ++;
            t_vis[ b[k] ] = 0;
        }
        for(int j=i;j<n;++j) {
            ans = max( ans , num + cnt[ b[j] ] );
            cnt[ b[j] ] = 0;
        }
    }
    cout<<ans<<endl;

    return 0;
}


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

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define debug(x) cout<<#x<<"="<<x<<endl;

const int MAX_NUM = 1000010;

bool t_vis[MAX_NUM],flag[MAX_NUM];
int cnt[MAX_NUM];
bool tt[MAX_NUM];
int b[4444];

int main()
{
    int n;
    cin>>n;rep(i,n)cin>>b[i];

    int ans = 0;
    rep(i,n) cnt[ b[i] ] ++;
    rep(i,n) {
        ans = max(ans , cnt[ b[i] ]);
        cnt[ b[i] ] = 0;
    }
    rep(i,n) if(!flag[ b[i] ]) {
        flag[ b[i] ] = 1;
        int num = 0, las = i;
        for(int j=i;j<n;++j) {
            if( b[j] == b[i] ) {
                num ++;
                for(int k=las;k<j;++k) if(t_vis[ b[k] ]){
                    cnt[ b[k] ] ++;
                    t_vis[ b[k] ] = 0;
                }
                las = j+1;
                continue;
            }
            t_vis[ b[j] ] ++;
        }
        for(int k=i;k<las;++k) if( b[k] != b[i] && !tt[ b[k] ]) {
            cnt[ b[k] ] = cnt[ b[k] ] + cnt[ b[k] ] + 1;
            tt[ b[k] ] = 1;
        }
        for(int k=las;k<n;++k) if(t_vis[ b[k] ]){
            cnt[ b[k] ] ++;
            if( cnt[ b[k] ] == 1 ) cnt[ b[k] ] = 2;
            t_vis[ b[k] ] = 0;
        }
        for(int j=i;j<n;++j) {
            ans = max( ans , cnt[ b[j] ] );
            cnt[ b[j] ] = 0;
            tt[ b[j] ] = 0;
        }
    }
    cout<<ans<<endl;

    return 0;
}


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

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define debug(x) cout<<#x<<"="<<x<<endl;

const int MAX_NUM = 100010;

bool t_vis[MAX_NUM],flag[MAX_NUM];
int cnt[MAX_NUM];
bool tt[MAX_NUM];
int b[4444];

int main()
{
    int n;
    cin>>n;rep(i,n)cin>>b[i];

    int ans = 0;
    rep(i,n) cnt[ b[i] ] ++;
    rep(i,n) {
        ans = max(ans , cnt[ b[i] ]);
        cnt[ b[i] ] = 0;
    }
    rep(i,n) if(!flag[ b[i] ]) {
        flag[ b[i] ] = 1;
        int num = 0, las = i;
        for(int j=i;j<n;++j) {
            if( b[j] == b[i] ) {
                num ++;
                for(int k=las;k<j;++k) if(t_vis[ b[k] ]){
                    cnt[ b[k] ] ++;
                    t_vis[ b[k] ] = 0;
                }
                las = j+1;
                continue;
            }
            t_vis[ b[j] ] ++;
        }
        for(int k=i;k<las;++k) if( b[k] != b[i] && !tt[ b[k] ]) {
            cnt[ b[k] ] = cnt[ b[k] ] + cnt[ b[k] ] + 1;
            tt[ b[k] ] = 1;
        }
        for(int k=las;k<n;++k) if(t_vis[ b[k] ]){
            cnt[ b[k] ] ++;
            if( cnt[ b[k] ] == 1 ) cnt[ b[k] ] = 2;
            t_vis[ b[k] ] = 0;
        }
        for(int j=i;j<n;++j) {
            ans = max( ans , cnt[ b[j] ] );
            cnt[ b[j] ] = 0;
            tt[ b[j] ] = 0;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;

int n , x , y , b , ans , c[4001] ;
vector<int> a[1000001] ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n && scanf("%d",&c[i]) ; i++) a[c[i]].push_back(i) ;
    for(int i=1 ; i<=n ; i++) for(int j=i+1 ; j<=n ; j++)
    {
       x=c[i] , y=c[j] , b ;
       int ptr1=i , ptr2=j , z=2 ;
       while(1)
       {
          if(z%2==0)
          {
             b=upper_bound(a[x].begin(),a[x].end(),ptr2)-a[x].begin() ;
             if(b==a[x].size()) break ;
             ptr1=a[x][b]+1 ;
          }
          else
          {
             b=upper_bound(a[y].begin(),a[y].end(),ptr1)-a[y].begin() ;
             if(b==a[y].size()) break ;
             ptr2=a[x][b]+1 ;
          }
          z++ ;
       }
       ans=max(ans,z) ;
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

int n , x , y , b , ans=1 , c[4001] ;
vector<int> a[1000001] ;
#define pii pair<int,int>
set<pii> S ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n && scanf("%d",&c[i]) ; i++) a[c[i]].push_back(i) ;
    for(int i=1 ; i<=n ; i++) for(int j=i+1 ; j<=n ; j++)
    {
       //if(S.find(make_pair(c[i],c[j]))!=S.end()) continue ;
       x=c[i] , y=c[j] , b ;
       int ptr1=i , ptr2=j , z=2 ;
       while(1)
       {
          if(z%2==0)
          {
             b=upper_bound(a[x].begin(),a[x].end(),ptr2)-a[x].begin() ;
             if(b==a[x].size()) break ;
             ptr1=a[x][b] ;
          }
          else
          {
             b=upper_bound(a[y].begin(),a[y].end(),ptr1)-a[y].begin() ;
             if(b==a[y].size()) break ;
             ptr2=a[y][b] ;
          }
          z++ ;
       }
       ans=max(ans,z) ; //S.insert(make_pair(c[i],c[j])) ;
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;

int n , ans=1 , a[4001] , f[1000001] , cnt[1000001] ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]) ;
    for(int i=1 ; i<=n ; i++)
    {
      int x=1 , prv=i , sig=0 ;
      memset(cnt,0,sizeof(cnt)) ; memset(f,0,sizeof(f)) ;
      for(int j=i+1 ; j<=n ; j++)
      {
         if(a[j]==a[i]) { prv=j ; x++ ; if(sig) ans++ ; ans=max(ans,x) ; sig=0 ; continue ; }
         if(f[a[j]]<prv)
         {
             f[a[j]]=j ; cnt[a[j]]++ ;
             if(ans<=2*cnt[a[j]]) ans=2*cnt[a[j]],sig=1 ;
         }
      }
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

int n , x , y , b , ans=1 , c[4001] ;
vector<int> a[1000001] ;
#define pii pair<int,int>
set<pii> S ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n && scanf("%d",&c[i]) ; i++) a[c[i]].push_back(i) ;
    for(int i=1 ; i<=n ; i++) for(int j=i+1 ; j<=n ; j++)
    {
       if(S.find(make_pair(c[i],c[j]))!=S.end()) continue ;
       x=c[i] , y=c[j] , b ;
       int ptr1=i , ptr2=j , z=2 ;
       while(1)
       {
          if(z%2==0)
          {
             b=upper_bound(a[x].begin(),a[x].end(),ptr2)-a[x].begin() ;
             if(b==a[x].size()) break ;
             ptr1=a[x][b] ;
          }
          else
          {
             b=upper_bound(a[y].begin(),a[y].end(),ptr1)-a[y].begin() ;
             if(b==a[y].size()) break ;
             ptr2=a[y][b] ;
          }
          z++ ;
       }
       ans=max(ans,z) ; S.insert(make_pair(c[i],c[j])) ;
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;

int n , x , y , b , ans=1 , c[4001] ;
vector<int> a[1000001] ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n && scanf("%d",&c[i]) ; i++) a[c[i]].push_back(i) ;
    for(int i=1 ; i<=n ; i++) for(int j=i+1 ; j<=n ; j++)
    {
       x=c[i] , y=c[j] , b ;
       int ptr1=i , ptr2=j , z=2 ;
       while(1)
       {
          if(z%2==0)
          {
             b=upper_bound(a[x].begin(),a[x].end(),ptr2)-a[x].begin() ;
             if(b==a[x].size()) break ;
             ptr1=a[x][b]+1 ;
          }
          else
          {
             b=upper_bound(a[y].begin(),a[y].end(),ptr1)-a[y].begin() ;
             if(b==a[y].size()) break ;
             ptr2=a[x][b]+1 ;
          }
          z++ ;
       }
       ans=max(ans,z) ;
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

int n , x , y , b , ans=1 , c[4001] , d[1000001] ;
vector<int> a[4001] ;
#define pii pair<int,int>
set<pii> S ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n && scanf("%d",&c[i]) ; i++)
    {
         if(!d[c[i]]) d[c[i]]=i ;
         a[d[c[i]]].push_back(i) ;
    }
    for(int i=1 ; i<=n ; i++) for(int j=i+1 ; j<=n ; j++)
    {
       if(S.find(make_pair(c[i],c[j]))!=S.end()) continue ;
       if(S.find(make_pair(c[j],c[i]))!=S.end()) continue ;
       x=d[c[i]] , y=d[c[j]] , b ;
       int ptr1=i , ptr2=j , z=2 ;
       while(1)
       {
          if(z%2==0)
          {
             b=upper_bound(a[x].begin(),a[x].end(),ptr2)-a[x].begin() ;
             if(b==a[x].size()) break ;
             ptr1=a[x][b] ;
          }
          else
          {
             b=upper_bound(a[y].begin(),a[y].end(),ptr1)-a[y].begin() ;
             if(b==a[y].size()) break ;
             ptr2=a[y][b] ;
          }
          z++ ;
       }
       ans=max(ans,z) ; S.insert(make_pair(c[i],c[j])) ;
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;

int n , ans=1 , a[4001] , f[1000001] , cnt[1000001] ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]) ;
    for(int i=1 ; i<=n ; i++)
    {
      int x=0 , prv=i , sig=0 ;
      memset(cnt,0,sizeof(cnt)) ; memset(f,0,sizeof(f)) ;
      for(int j=i+1 ; j<=n ; j++)
      {
         if(a[j]==a[i]) { prv=j ; x++ ; if(sig) ans++ ; sig=0 ; continue ; }
         if(f[a[j]]<prv)
         {
             f[a[j]]=j ; cnt[a[j]]++ ;
             if(ans<2*cnt[a[j]]) ans=2*cnt[a[j]],sig=1 ;
         }
      }
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;

int n , ans=1 , a[4001] , f[1000001] , cnt[1000001] ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]) ;
    for(int i=1 ; i<=n ; i++)
    {
      int x=0 , prv=i , sig=0 ;
      memset(cnt,0,sizeof(cnt)) ; memset(f,0,sizeof(f)) ;
      for(int j=i+1 ; j<=n ; j++)
      {
         if(a[j]==a[i]) { prv=j ; x++ ; if(sig) ans++ ; ans=max(ans,x) ; sig=0 ; continue ; }
         if(f[a[j]]<prv)
         {
             f[a[j]]=j ; cnt[a[j]]++ ;
             if(ans<=2*cnt[a[j]]) ans=2*cnt[a[j]],sig=1 ;
         }
      }
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

int n , x , y , b , ans=1 , c[4001] ;
vector<int> a[1000001] ;
#define pii pair<int,int>
set<pii> S ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n && scanf("%d",&c[i]) ; i++) a[c[i]].push_back(i) ;
    for(int i=1 ; i<=n ; i++) for(int j=i+1 ; j<=n ; j++)
    {
       if(S.find(make_pair(c[i],c[j]))!=S.end()) continue ;
       if(S.find(make_pair(c[j],c[j]))!=S.end()) continue ;
       x=c[i] , y=c[j] , b ;
       int ptr1=i , ptr2=j , z=2 ;
       while(1)
       {
          if(z%2==0)
          {
             b=upper_bound(a[x].begin(),a[x].end(),ptr2)-a[x].begin() ;
             if(b==a[x].size()) break ;
             ptr1=a[x][b] ;
          }
          else
          {
             b=upper_bound(a[y].begin(),a[y].end(),ptr1)-a[y].begin() ;
             if(b==a[y].size()) break ;
             ptr2=a[y][b] ;
          }
          z++ ;
       }
       ans=max(ans,z) ; S.insert(make_pair(c[i],c[j])) ;
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;

int n , ans=1 , a[4001] , f[1000001] , cnt[1000001] ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]) ;
    for(int i=1 ; i<=n ; i++)
    {
      int x=0 , prv=i , sig=0 ;
      memset(cnt,0,sizeof(cnt)) ; memset(f,0,sizeof(f)) ;
      for(int j=i+1 ; j<=n ; j++)
      {
         if(a[j]==a[i]) { prv=j ; x++ ; if(sig) ans++ ; ans=max(ans,x) ; sig=0 ; continue ; }
         if(f[a[j]]<prv)
         {
             f[a[j]]=j ; cnt[a[j]]++ ;
             if(ans<2*cnt[a[j]]) ans=2*cnt[a[j]],sig=1 ;
         }
      }
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;

int n , x , y , b , ans=1 , c[4001] ;
vector<int> a[1000001] ;
#define pii pair<int,int>
set<pii> S ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n && scanf("%d",&c[i]) ; i++) a[c[i]].push_back(i) ;
    for(int i=1 ; i<=n ; i++) for(int j=i+1 ; j<=n ; j++)
    {
       if(S.find(make_pair(c[i],c[j]))!=S.end()) continue ;
       x=c[i] , y=c[j] , b ;
       int ptr1=i , ptr2=j , z=2 ;
       while(1)
       {
          if(z%2==0)
          {
             b=upper_bound(a[x].begin(),a[x].end(),ptr2)-a[x].begin() ;
             if(b==a[x].size()) break ;
             ptr1=a[x][b]+1 ;
          }
          else
          {
             b=upper_bound(a[y].begin(),a[y].end(),ptr1)-a[y].begin() ;
             if(b==a[y].size()) break ;
             ptr2=a[x][b]+1 ;
          }
          z++ ;
       }
       ans=max(ans,z) ; S.insert(make_pair(c[i],c[j])) ;
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;

int n , ans=1 , a[4001] , f[1000001] , cnt[1000001] ;

int main()
{
    scanf("%d",&n) ;
    for(int i=1 ; i<=n ; i++) scanf("%d",&a[i]) ;
    for(int i=1 ; i<=n ; i++)
    {
      int x=0 , prv=i , sig=0 ;
      memset(cnt,0,sizeof(cnt)) ; memset(f,0,sizeof(f)) ;
      for(int j=i+1 ; j<=n ; j++)
      {
         if(a[j]==a[i]) { prv=j ; x++ ; if(sig) ans++ ; sig=0 ; continue ; }
         if(f[a[j]]<prv)
         {
             f[a[j]]=j ; cnt[a[j]]++ ;
             if(ans<=2*cnt[a[j]]) ans=2*cnt[a[j]],sig=1 ;
         }
      }
    }
    printf("%d\n",ans) ;

 return 0 ;
}


#include <cstdio>
#include <algorithm>
#include <utility>
#include <cstring>
#define REP(i,n) for (int i=1;i<=n;++i)
using namespace std;

int n,now,big,ans,a[4040],last[4040],sum[4040];
pair<int,int> b[4040];
bool boo[4040];

int main() {
	scanf("%d",&n);
	REP(i,n) scanf("%d",&a[i]);
	REP(i,n) b[i]=make_pair(a[i],i);
	sort(b+1,b+n+1);
	big=0; now=0;
	REP(i,n) {
		if (big!=b[i].first) big=b[i].first,++now;
		a[b[i].second]=now;
	}
	REP(i,n) {
		if (boo[a[i]]) continue;
		boo[a[i]]=true;
		memset(last,127,sizeof(last));
		memset(sum,0,sizeof(sum));
		now=0;
		REP(j,n)
			if (a[j]==a[i]) ++now;
			else
				if (last[a[j]]!=now) {
					if (!now) --sum[a[j]];
					sum[a[j]]+=2;last[a[j]]=now;
				}
		REP(j,n) if (last[j]!=now) ++sum[j];
		REP(j,n) ans=max(ans,sum[j]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <utility>
#include <cstring>
#define REP(i,n) for (int i=1;i<=n;++i)
using namespace std;

int n,now,big,ans,a[4040],last[4040],sum[4040];
pair<int,int> b[4040];
bool boo[4040];

int main() {
	scanf("%d",&n);
	REP(i,n) scanf("%d",&a[i]);
	REP(i,n) b[i]=make_pair(a[i],i);
	sort(b+1,b+n+1);
	big=0; now=0;
	REP(i,n) {
		if (big!=b[i].first) big=b[i].first,++now;
		a[b[i].second]=now;
	}
	REP(i,n) {
		if (boo[a[i]]) continue;
		boo[a[i]]=true;
		memset(last,127,sizeof(last));
		memset(sum,0,sizeof(sum));
		now=0;
		REP(j,n)
			if (a[j]==a[i]) ++now;
			else if (a[j]<a[i])
				if (last[a[j]]!=now) {
					if (!now) --sum[a[j]];
					sum[a[j]]+=2;last[a[j]]=now;
				}
		REP(j,n) if (last[j]!=now) ++sum[j];
		REP(j,n) ans=max(ans,sum[j]);
	}
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#define REP(i,n) for (int i=1;i<=n;++i)
using namespace std;

int n,ans,a[4040];

int JS(int k) {
	for (int i=k;1;++i)
		if ((i>n)||(a[k+((i-k)&1)]!=a[i])) return i-k;
}

int main() {
	scanf("%d",&n);
	REP(i,n) scanf("%d",&a[i]);
	REP(i,n) ans=max(ans,JS(i));
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <utility>
#include <cstring>
#define REP(i,n) for (int i=1;i<=n;++i)
using namespace std;

int n,now,big,ans,a[4040],last[4040],sum[4040];
pair<int,int> b[4040];
bool boo[4040];

int main() {
	scanf("%d",&n);
	REP(i,n) scanf("%d",&a[i]);
	REP(i,n) b[i]=make_pair(a[i],i);
	sort(b+1,b+n+1);
	big=0; now=0;
	REP(i,n) {
		if (big!=b[i].first) big=b[i].first,++now;
		a[b[i].second]=now;
	}
	REP(i,n) {
		if (boo[a[i]]) continue;
		boo[a[i]]=true;
		memset(last,127,sizeof(last));
		memset(sum,0,sizeof(sum));
		now=0;
		REP(j,n)
			if (a[j]==a[i]) ++now;
			else
				if (last[a[j]]!=now) {
					if (!now) --sum[a[j]];
					sum[a[j]]+=2;last[a[j]]=now;
				}
		ans=max(ans,now);
		REP(j,n) if (last[j]!=now) ++sum[j];
		REP(j,n) ans=max(ans,sum[j]);
	}
	printf("%d\n",ans);
	return 0;
}