import java.util.*;
import java.io.*;
import java.awt.Point;
import java.math.BigDecimal;
import java.math.BigInteger;

import static java.lang.Math.*;

// Solution is at the bottom of code

public class B implements Runnable{

	final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

	BufferedReader in;
	OutputWriter out;
	StringTokenizer tok = new StringTokenizer("");

	public static void main(String[] args){
		new Thread(null, new B(), "", 128 * (1L << 20)).start();
	}

	/////////////////////////////////////////////////////////////////////

	void init() throws FileNotFoundException{
		Locale.setDefault(Locale.US);

		if (ONLINE_JUDGE){
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new OutputWriter(System.out);
		}else{
			in = new BufferedReader(new FileReader("input.txt"));
			out = new OutputWriter("output.txt");
		}
	}

	////////////////////////////////////////////////////////////////

	long timeBegin, timeEnd;

	void time(){
		timeEnd = System.currentTimeMillis();
		System.err.println("Time = " + (timeEnd - timeBegin));
	}

	void debug(Object... objects){
		if (ONLINE_JUDGE){
			for (Object o: objects){
				System.err.println(o.toString());
			}
		}
	}

	/////////////////////////////////////////////////////////////////////

	public void run(){
		try{
			timeBegin = System.currentTimeMillis();
			Locale.setDefault(Locale.US);

			init();
			solve();

			out.close();
			time();
		}catch (Exception e){
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}

	/////////////////////////////////////////////////////////////////////

	String delim = " ";

	String readString() throws IOException{
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(in.readLine());
			}catch (Exception e){
				return null;
			}
		}

		return tok.nextToken(delim);
	}

	String readLine() throws IOException{
		return in.readLine();
	}

	/////////////////////////////////////////////////////////////////

	final char NOT_A_SYMBOL = '\0';

	char readChar() throws IOException{
		int intValue = in.read();

		if (intValue == -1){
			return NOT_A_SYMBOL;
		}

		return (char) intValue;
	}

	char[] readCharArray() throws IOException{
		return readLine().toCharArray();
	}

	/////////////////////////////////////////////////////////////////

	int readInt() throws IOException{
		return Integer.parseInt(readString());
	}

	int[] readIntArray(int size) throws IOException{
		int[] array = new int[size];

		for (int index = 0; index < size; ++index){
			array[index] = readInt();
		}

		return array;
	}

	///////////////////////////////////////////////////////////////////

	long readLong() throws IOException{
		return Long.parseLong(readString());
	}

	long[] readLongArray(int size) throws IOException{
		long[] array = new long[size];

		for (int index = 0; index < size; ++index){
			array[index] = readLong();
		}

		return array;
	}

	////////////////////////////////////////////////////////////////////

	double readDouble() throws IOException{
		return Double.parseDouble(readString());
	}

	double[] readDoubleArray(int size) throws IOException{
		double[] array = new double[size];

		for (int index = 0; index < size; ++index){
			array[index] = readDouble();
		}

		return array;
	}

	////////////////////////////////////////////////////////////////////

	BigInteger readBigInteger() throws IOException {
		return new BigInteger(readString());
	}

	BigDecimal readBigDecimal() throws IOException {
		return new BigDecimal(readString());
	}

	/////////////////////////////////////////////////////////////////////

	Point readPoint() throws IOException{
		int x = readInt();
		int y = readInt();
		return new Point(x, y);
	}

	Point[] readPointArray(int size) throws IOException{
		Point[] array = new Point[size];

		for (int index = 0; index < size; ++index){
			array[index] = readPoint();
		}

		return array;
	}

	/////////////////////////////////////////////////////////////////////

	List<Integer>[] readGraph(int vertexNumber, int edgeNumber)
	throws IOException{
		@SuppressWarnings("unchecked")
		List<Integer>[] graph = new List[vertexNumber];

		for (int index = 0; index < vertexNumber; ++index){
			graph[index] = new ArrayList<Integer>();
		}

		while (edgeNumber-- > 0){
			int from = readInt() - 1;
			int to = readInt() - 1;

			graph[from].add(to);
			graph[to].add(from);
		}

		return graph;
	}

	/////////////////////////////////////////////////////////////////////

	static class IntIndexPair {

		static Comparator<IntIndexPair> increaseComparator = new Comparator<B.IntIndexPair>() {

			@Override
			public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
				int value1 = indexPair1.value;
				int value2 = indexPair2.value;

				if (value1 != value2) return value1 - value2;

				int index1 = indexPair1.index;
				int index2 = indexPair2.index;

				return index1 - index2;
			}
		};

		static Comparator<IntIndexPair> decreaseComparator = new Comparator<B.IntIndexPair>() {

			@Override
			public int compare(IntIndexPair indexPair1, IntIndexPair indexPair2) {
				int value1 = indexPair1.value;
				int value2 = indexPair2.value;

				if (value1 != value2) return -(value1 - value2);

				int index1 = indexPair1.index;
				int index2 = indexPair2.index;

				return index1 - index2;
			}
		};

		int value, index;

		public IntIndexPair(int value, int index) {
			super();
			this.value = value;
			this.index = index;
		}
	}

	IntIndexPair[] readIntIndexArray(int size) throws IOException {
		IntIndexPair[] array = new IntIndexPair[size];

		for (int index = 0; index < size; ++index) {
			array[index] = new IntIndexPair(readInt(), index);
		}

		return array;
	}

	/////////////////////////////////////////////////////////////////////

	static class OutputWriter extends PrintWriter{

		final int DEFAULT_PRECISION = 12;

		int precision;
		String format, formatWithSpace;

		{
			precision = DEFAULT_PRECISION;

			format = createFormat(precision);
			formatWithSpace = format + " ";
		}

		public OutputWriter(OutputStream out) {
			super(out);
		}

		public OutputWriter(String fileName) throws FileNotFoundException {
			super(fileName);
		}

		public int getPrecision() {
			return precision;
		}

		public void setPrecision(int precision) {
			precision = max(0, precision);
			this.precision = precision;

			format = createFormat(precision);
			formatWithSpace = format + " ";
		}

		private String createFormat(int precision){
			return "%." + precision + "f";
		}

		@Override
		public void print(double d){
			printf(format, d);
		}

		public void printWithSpace(double d){
			printf(formatWithSpace, d);
		}

		public void printAll(double...d){
			for (int i = 0; i < d.length - 1; ++i){
				printWithSpace(d[i]);
			}

			print(d[d.length - 1]);
		}

		@Override
		public void println(double d){
			printlnAll(d);
		}

		public void printlnAll(double... d){
			printAll(d);
			println();
		}
	}

	/////////////////////////////////////////////////////////////////////

	int[][] steps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
	int[][] steps8 = {
			{-1, 0}, {1, 0}, {0, -1}, {0, 1},
			{-1, -1}, {1, 1}, {1, -1}, {-1, 1}
	};

	boolean check(int index, int lim){
		return (0 <= index && index < lim);
	}

	/////////////////////////////////////////////////////////////////////

	boolean checkBit(int mask, int bitNumber){
		return (mask & (1 << bitNumber)) != 0;
	}

	/////////////////////////////////////////////////////////////////////

	void solve() throws IOException {
		int n = readInt();

		Point[] cards = readPointArray(n);

		int answer = getScore(n, cards);
		out.println(answer);
	}

	int getScore(int n, Point[] cards) {
		Arrays.sort(cards, new Comparator<Point>() {

			@Override
			public int compare(Point card1, Point card2) {
				if (card1.y != card2.y) return card2.y - card1.y;

				return card2.x - card1.x;
			}
		});

		int score = 0;
		for (int i = 0, cardCount = 1; i < n && cardCount > 0; ++i, cardCount--) {
			score += cards[i].x;
			cardCount += cards[i].y;
		}

		return score;
	}
}


import java.util.Scanner;

public class P155B_Combination {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] top = new int[n];
		int[] bottom = new int[n];

		for (int i = 0; i < n; i++) {
			top[i] = scanner.nextInt();
			bottom[i] = scanner.nextInt();
		}

		scanner.close();

		int temp = 0;

		for (int i = 0; i < n - 1; i++) {
			for (int j = i + 1; j < n; j++) {
				if ((bottom[i] < bottom[j])
						|| (bottom[i] == bottom[j] && top[i] < top[j])) {
					temp = bottom[i];
					bottom[i] = bottom[j];
					bottom[j] = temp;

					temp = top[i];
					top[i] = top[j];
					top[j] = temp;
				}
			}
		}

		int score = 0, ncards = 1, index = 0;

		while (ncards > 0 && index < n) {
			score += top[index];
			ncards += bottom[index] - 1;
			index++;
		}

		System.out.println(score);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class ProblemB {
	public static void main(String[] args) {
		InputReader in = new InputReader();
		PrintWriter out = new PrintWriter(System.out);

		new ProblemB().solve(in, out);

		out.close();
	}

	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt();

		List<Integer> zero = new ArrayList<Integer>();

		int count = 0;
		int total = 0;
		for (int i = 0; i < n; i++) {
			int a = in.nextInt();
			int b = in.nextInt();

			if (b == 0) {
				zero.add(a);
			} else {
				count += b;
				total += a;
			}
		}

		Collections.sort(zero);

		int i = zero.size() - 1;
		while (i >= 0 && count > 0) {
			total += zero.get(i);

			i--;
			count--;
		}

		if (zero.size() == n) {
			total = zero.get(n - 1);
		}

		out.println(total);
	}

	static class InputReader {
		public BufferedReader br;
		public StringTokenizer st;

		public InputReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

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

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class ProblemB {
	public static void main(String[] args) {
		InputReader in = new InputReader();
		PrintWriter out = new PrintWriter(System.out);

		new ProblemB().solve(in, out);

		out.close();
	}

	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt();

		List<Integer> zero = new ArrayList<Integer>();

		int count = 0;
		int total = 0;
		for (int i = 0; i < n; i++) {
			int a = in.nextInt();
			int b = in.nextInt();

			if (b == 0) {
				zero.add(a);
			} else {
				count += b - 1;
				total += a;
			}
		}

		Collections.sort(zero);

		int i = zero.size() - 1;
		while (i >= 0 && count >= 0) {
			total += zero.get(i);

			i--;
			count--;
		}

		if (zero.size() == n) {
			total = zero.get(n - 1);
		}

		out.println(total);
	}

	static class InputReader {
		public BufferedReader br;
		public StringTokenizer st;

		public InputReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

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

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class ProblemB {
	public static void main(String[] args) {
		InputReader in = new InputReader();
		PrintWriter out = new PrintWriter(System.out);

		new ProblemB().solve(in, out);

		out.close();
	}

	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt();

		List<Integer> zero = new ArrayList<Integer>();

		int count = 0;
		int total = 0;
		for (int i = 0; i < n; i++) {
			int a = in.nextInt();
			int b = in.nextInt();

			if (b == 0) {
				zero.add(a);
			} else {
				count += b;
				total += a;
			}
		}

		Collections.sort(zero);

		int i = zero.size() - 1;
		while (i >= 0 && count >= 0) {
			total += zero.get(i);

			i--;
			count--;
		}

		out.println(total);
	}

	static class InputReader {
		public BufferedReader br;
		public StringTokenizer st;

		public InputReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

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

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.util.*;
/**
 *
 * @author Hamzah
 */

public class Combination {
    private static final int TOP = 0;
    private static final int BOTTOM = 1;
private static int solve(int N, int[][] cards) {

        Arrays.sort(cards, new Comparator<int[]>() {

            @Override
            public int compare(int[] card1, int[] card2) {
                if (card1[BOTTOM] == card2[BOTTOM]) return card2[TOP] - card1[TOP];
                return card2[BOTTOM] - card1[BOTTOM];
            }
        });

        int score = 0, counter = 1;
        for (int i = 0; i < N && counter > 0; i++, counter--) {
            score += cards[i][TOP];
            counter += cards[i][BOTTOM];
        }

        return score;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int n,counter =1;
        Scanner sc = new Scanner(System.in);
        n= sc.nextInt();

        int a[][]=new int[n][2];
        for(int i=0;i<n;i++){
            a[i][0]=sc.nextInt();
             a[i][1]=sc.nextInt();
        }
        System.out.println(solve(n, a));
    }
}


import java.util.*;

public class Combination_155B {
    static int[] a;
    static int[] b;
    static int n;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while( input.hasNextInt() ){
            n = input.nextInt();

            a = new int[n];
            b = new int[n];

            for( int i = 0 ;i < n ;i++){
                a[i] = input.nextInt();
                b[i] = input.nextInt();

            }//end for.

            bubbleSort();

            int points = 0;
            int cards = 1;
            int loop = 0;

            while( cards > 0 ){
                points += a[loop];
                cards += b[loop];
                loop++;
                cards--;
            }//end while.

            System.out.println(points);
        }//end while input.

    }//end main.

    static void bubbleSort(){

        boolean swapFlag = true;
        int loopNo = 0;
        while( swapFlag ){
            swapFlag = false;
            for( int i = 0 ; i < n - 1 - loopNo ;i++){
                //el keber fo2.
                if( b[i] < b[i + 1] ){
                    int temp = b[i+1];
                    b[i + 1] = b[i];
                    b[i] = temp;

                    temp = a[i+1];
                    a[i + 1] = a[i];
                    a[i] = temp;
                    swapFlag = true;
                }//end swapping.
                else if ( b[i] == b[i + 1]){
                    if( a[i] < a[i + 1] ){
                        int temp = b[i+1];
                        b[i + 1] = b[i];
                        b[i] = temp;

                        temp = a[i+1];
                        a[i + 1] = a[i];
                        a[i] = temp;
                        swapFlag = true;
                    }//end swapping.
                }//end else if.

            }//end for.
            loopNo++;

        }//end while.

    }//end method sort.

}//end class.


import java.util.*;

public class Combination_155B {
    static int[] a;
    static int[] b;
    static int n;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while( input.hasNextInt() ){
            n = input.nextInt();

            a = new int[n];
            b = new int[n];

            for( int i = 0 ;i < n ;i++){
                a[i] = input.nextInt();
                b[i] = input.nextInt();

            }//end for.

            bubbleSort();

            int points = 0;
            int cards = 1;
            int loop = 0;

            while( cards > 0 && loop < n){
                points += a[loop];
                cards += b[loop];
                loop++;
                cards--;
            }//end while.

            System.out.println(points);
        }//end while input.

    }//end main.

    static void bubbleSort(){

        boolean swapFlag = true;
        int loopNo = 0;
        while( swapFlag ){
            swapFlag = false;
            for( int i = 0 ; i < n - 1 - loopNo ;i++){
                //el keber fo2.
                if( b[i] < b[i + 1] ){
                    int temp = b[i+1];
                    b[i + 1] = b[i];
                    b[i] = temp;

                    temp = a[i+1];
                    a[i + 1] = a[i];
                    a[i] = temp;
                    swapFlag = true;
                }//end swapping.
                else if ( b[i] == b[i + 1]){
                    if( a[i] < a[i + 1] ){
                        int temp = b[i+1];
                        b[i + 1] = b[i];
                        b[i] = temp;

                        temp = a[i+1];
                        a[i + 1] = a[i];
                        a[i] = temp;
                        swapFlag = true;
                    }//end swapping.
                }//end else if.

            }//end for.
            loopNo++;

        }//end while.

    }//end method sort.

}//end class.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Combination {
	public static void main(String args[]) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		PriorityQueue q = new PriorityQueue();
		for (int i=0;i<n;i++)
		{
			int a = sc.nextInt();
			int b = sc.nextInt();
			q.add(new Card(a,b));
		}
		int moves = 1;
		int points = 0;
		while (moves>0)
		{
			Card c = (Card) q.remove();
			points+=c.a;
			moves--;
			moves+=c.b;
		}
		System.out.println(points);
	}
	static class Card implements Comparable
	{
		int a;
		int b;
		Card(int a,int b)
		{
			this.a = a;
			this.b = b;
		}
		@Override
		public int compareTo(Object arg0) {
			Card c = (Card) arg0;
			if (this.b>c.b)
				return -1;
			if (this.b==c.b)
				return (this.a>c.a)? -1 : 1;
			if (this.b<c.b)
				return 1;
			return 0;
		}
	}
	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {

			br = new BufferedReader(new InputStreamReader(s));

		}

		public void close() throws IOException
		{
			br.close();
		}

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

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

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public boolean ready() throws IOException {
			return br.ready();
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

import trials.Dragons.Dragon;

public class Combination {
	public static void main(String args[]) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		PriorityQueue q = new PriorityQueue();
		for (int i=0;i<n;i++)
		{
			int a = sc.nextInt();
			int b = sc.nextInt();
			q.add(new Card(a,b));
		}
		int moves = 1;
		int points = 0;
		while (moves>0)
		{
			Card c = (Card) q.remove();
			points+=c.a;
			moves--;
			moves+=c.b;
		}
		System.out.println(points);
	}
	static class Card implements Comparable
	{
		int a;
		int b;
		Card(int a,int b)
		{
			this.a = a;
			this.b = b;
		}
		@Override
		public int compareTo(Object arg0) {
			Card c = (Card) arg0;
			if (this.b>c.b)
				return -1;
			if (this.b==c.b)
				return (this.a>c.a)? -1 : 1;
			if (this.b<c.b)
				return 1;
			return 0;
		}
	}
	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {

			br = new BufferedReader(new InputStreamReader(s));

		}

		public void close() throws IOException
		{
			br.close();
		}

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

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

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public boolean ready() throws IOException {
			return br.ready();
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Combination {
	public static void main(String args[]) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		PriorityQueue q = new PriorityQueue();
		for (int i=0;i<n;i++)
		{
			int a = sc.nextInt();
			int b = sc.nextInt();
			q.add(new Card(a,b));
		}
		int moves = 1;
		int points = 0;
		while (moves>0 && !q.isEmpty())
		{
			Card c = (Card) q.remove();
			points+=c.a;
			moves--;
			moves+=c.b;
		}
		System.out.println(points);
	}
	static class Card implements Comparable
	{
		int a;
		int b;
		Card(int a,int b)
		{
			this.a = a;
			this.b = b;
		}
		@Override
		public int compareTo(Object arg0) {
			Card c = (Card) arg0;
			if (this.b>c.b)
				return -1;
			if (this.b==c.b)
				return (this.a>c.a)? -1 : 1;
			if (this.b<c.b)
				return 1;
			return 0;
		}
	}
	static class Scanner
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {

			br = new BufferedReader(new InputStreamReader(s));

		}

		public void close() throws IOException
		{
			br.close();
		}

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

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

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public boolean ready() throws IOException {
			return br.ready();
		}
	}
}


//package codeforces;

import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

/**
 * Created by nitin.s on 14/03/16.
 */
public class Combination {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Queue<Integer> q = new PriorityQueue<Integer>(1, Collections.reverseOrder());
        int answer = 0;
        int takes = 1;
        for(int i = 0; i < n; ++i) {
            int a = in.nextInt();
            int b = in.nextInt();
            if(b > 0) {
                takes += (b - 1);
                answer += a;
            } else {
                q.offer(a);
            }
        }
        for(int i = 0; i < takes && !q.isEmpty(); ++i) {
            answer += q.poll();
        }
        System.out.println(answer);
    }
}


import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.exit;
import static java.util.Arrays.fill;

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import java.text.*;

public class Main {
	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer tok;

	final static double EPS = 1e-9;
	final static int MOD = 1000000009;

	static void solve() throws Exception {
		int n = nextInt();
		Pair[] a = new Pair[n];
		for (int i = 0; i < n; i++) {
			a[i] = new Pair(nextInt(), nextInt());
		}
		Arrays.sort(a);
		int res = 0;
		int cnt = 1;
		for (int i = n-1; i >= 0; i--) {
			res += a[i].first;
			cnt = cnt-1 + a[i].second;
			if (cnt == 0) {
				break;
			}
		}
		out.println(res);
	}

	static class Pair implements Comparable<Pair> {
		int first, second;

		Pair(int first, int second) {
			this.first = first;
			this.second = second;
		}

		public int compareTo(Pair other) {
			if (Integer.compare(second, other.second) == 0) {
				return Integer.compare(first, other.first);
			} else {
				return Integer.compare(second, other.second);
			}
		}
	}

	static long sqr(long x) {
		return x*x;
	}

	static int nextInt() throws IOException {
		return parseInt(next());
	}

	static long nextLong() throws IOException {
		return parseLong(next());
	}

	static double nextDouble() throws IOException {
		return parseDouble(next());
	}

	static BigInteger nextBigInteger() throws IOException {
		return new BigInteger(next());
	}

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

	public static void main(String args[]) {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(new OutputStreamWriter(System.out));
			solve();
			in.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			exit(1);
		}
	}
}


import java.io.*;
import java.util.*;
public class Combination {
	static class Card implements Comparable<Card>{
		public int ai;
		public int bi;
		public Card(int a, int b) {
			ai = a;
			bi = b;
		}
		public int compareTo(Card x) {
			int c = x.bi - bi;
			return (c == 0) ? (x.ai - ai) : c;
		}
	}
	public static void main(String[] args) {
		List<Card> list = new ArrayList<Card>();
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		for(int i = 0; i < n; i++) {
			list.add(new Card(in.nextInt(), in.nextInt()));
		}
		Collections.sort(list);
		int res = list.get(0).ai;
		int counter = list.get(0).bi;
		for(int i = 0; i < counter; i++) {
			Card C = list.get(i + 1);
			res += C.ai;
			counter = counter + C.bi;
		}
		System.out.println(res);
	}
}


import java.io.*;
import java.util.*;
public class Combination {
	static class Card implements Comparable<Card>{
		public int ai;
		public int bi;
		public Card(int a, int b) {
			ai = a;
			bi = b;
		}
		public int compareTo(Card x) {
			int c = x.bi - bi;
			return (c == 0) ? (x.ai - ai) : c;
		}
	}
	public static void main(String[] args) {
		List<Card> list = new ArrayList<Card>();
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		for(int i = 0; i < n; i++) {
			list.add(new Card(in.nextInt(), in.nextInt()));
		}
		Collections.sort(list);
		int res = list.get(0).ai;
		int counter = list.get(0).bi;
		for(int i = 0; i < counter && i < list.size() - 1; i++) {
			Card C = list.get(i + 1);
			res += C.ai;
			counter = counter + C.bi;
		}
		System.out.println(res);
	}
}


import java.io.*;
import java.util.*;
public class B {
	static Card[] cards;
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		PriorityQueue<Card> q = new PriorityQueue<Card>();
		for(int i = 0; i < N; i++)
			q.add(new Card(in.nextInt(), in.nextInt()));

		int counter = 1;
		int points = 0;
		while(counter > 0 && !q.isEmpty()) {
			counter--;
			Card at = q.poll();
			points += at.a;
			counter += at.b;
		}
		System.out.println(points);

	}

	public static class Card implements Comparable<Card> {
		int a, b;
		public Card(int a, int b) {
			this.a = a; this.b = b;
		}

		public int compareTo(Card that) {
			if(this.b == that.b)
				return that.a - this.a;
			return that.b - this.b;
		}
	}
}


    import java.awt.*;
    import java.awt.geom.*;
    import java.io.*;
import java.math.BigInteger;
    import java.text.*;
    import java.util.*;
import java.util.regex.*;

    public class Main{
        static class Run implements Runnable{
            //TODO parameters
            final boolean consoleIO = true;
            final String inFile = "input.txt";
            final String outFile = "output.txt";

            int n;
            Card[] cards;

            public void run() {
                n = nextInt();
                cards = new Card[n];

                for(int i = 0; i < n; ++i)
                    cards[i] = new Card(nextInt(), nextInt());

                Arrays.sort(cards, new CardComparator());

                int remains = 1;
                int count = 0;
                int points = 0;

                while(count<n && remains>0) {
                    Card c = cards[count];

                    points += c.a;
                    remains = remains-1+c.b;

                    ++count;
                }

                print(points);
                close();
            }

            class CardComparator implements Comparator<Card> {
                @Override
                public int compare(Card c1, Card c2) {
                    if(c1.b > c2.b)
                        return -1;

                    if(c1.b < c2.b)
                        return 1;

                    return Integer.compare(c2.a, c1.a);
                }
            }

            class Card {
                int a, b;

                Card(int a, int b) {
                    this.a = a;
                    this.b = b;
                }
            }
        //=========================================================================================================================
            BufferedReader in;
            PrintWriter out;
            StringTokenizer strTok;

            Run() {
                if (consoleIO) {
                    initConsoleIO();
                }
                else {
                    initFileIO();
                }
            }

            void initConsoleIO() {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(new OutputStreamWriter(System.out));
            }

            void initFileIO() {
                try {
                    in = new BufferedReader(new FileReader(inFile));
                    out = new PrintWriter(new FileWriter(outFile));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            void close() {
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

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

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

            float nextFloat() {
                return Float.parseFloat(nextToken());
            }

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

            String nextLine() {
                try {
                    return in.readLine();
                } catch (IOException e) {
                    return "__NULL";
                }
            }

            boolean hasMoreTokens() {
                return (strTok == null) || (strTok.hasMoreTokens());
            }

            String nextToken() {
                while (strTok == null || !strTok.hasMoreTokens()) {
                    String line;
                    try {
                        line = in.readLine();
                        strTok = new StringTokenizer(line);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                return strTok.nextToken();
            }

            void cout(Object o){
                System.out.println(o);
            }

            void print(Object o) {
                out.write(o.toString());
            }

            void println(Object o) {
                out.write(o.toString() + '\n');
            }

            void printf(String format, Object... args) {
                out.printf(format, args);
            }

            String sprintf(String format, Object... args) {
            return MessageFormat.format(format, args);
        }
        }

        static class Pair<A, B> {
            A a;
            B b;

            A f() {
                return a;
            }

            B s() {
                return b;
            }

            Pair(A a, B b) {
                this.a = a;
                this.b = b;
            }

            Pair(Pair<A, B> p) {
                a = p.f();
                b = p.s();
            }
        }

        public static void main(String[] args) throws IOException {
            Run run = new Run();
            Thread thread = new Thread(run);
            thread.run();
        }
    }


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

public class Main8 {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		ArrayList<Integer> cad_0 = new ArrayList<>();
		int cont = 0;
		int repete = 0;
		int max = 0;
		for (int i = 0; i < n; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			if( b == 0 ){
				if(max<a)
					max = a;
				cad_0.add(a);
				continue;
			}else{
				cont += a;
				repete += b;
			}
		}
		if(repete!=0){
			int[] cad_1 = new int[cad_0.size()];
			int asdasd = cad_0.size();
			for (int i = 0; i < asdasd; i++)
				cad_1[i] = cad_0.remove(0);
			Arrays.sort(cad_1);
			for (int i = 0; i < cad_1.length; i++)
				cad_0.add(cad_1[i]);
			for (int i = repete-1; i >=0; i--) {
				if(cad_0.size()!=0)
				cont+=cad_0.remove(cad_0.size()-1);
			}
		}else cont = max;
		System.out.println(cont);
	}

}


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

public class Main8 {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		ArrayList<Integer> cad_0 = new ArrayList<>();
		int cont = 0;
		int repete = 0;
		int max = 0;
		for (int i = 0; i < n; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			if( b == 0 ){
				if(max<a)
					max = a;
				cad_0.add(a);
				continue;
			}else{
				cont += a;
				repete += b;
			}
		}
		if(repete!=0){
			int[] cad_1 = new int[cad_0.size()];
			for (int i = 0; i <= cad_0.size(); i++)
				cad_1[i] = cad_0.remove(0);
			Arrays.sort(cad_1);
			for (int i = 0; i < cad_1.length; i++)
				cad_0.add(cad_1[i]);
			for (int i = repete-1; i >=0; i--) {
				cont+=cad_0.remove(cad_0.size()-1);
			}
		}else cont = max;
		System.out.println(cont);
	}

}


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

public class Main8 {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		ArrayList<Integer> cad_0 = new ArrayList<>();
		int cont = 0;
		int repete = 0;
		int max = 0;
		for (int i = 0; i < n; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			if( b == 0 ){
				if(max<a)
					max = a;
				cad_0.add(a);
				continue;
			}else{
				cont += a;
				if(repete==0)
				repete += b;
				else repete += b-1;
			}
		}
		if(repete!=0){
			int[] cad_1 = new int[cad_0.size()];
			int asdasd = cad_0.size();
			for (int i = 0; i < asdasd; i++)
				cad_1[i] = cad_0.remove(0);
			Arrays.sort(cad_1);
			for (int i = 0; i < cad_1.length; i++)
				cad_0.add(cad_1[i]);
			for (int i = repete-1; i >=0; i--) {
				if(cad_0.size()!=0)
				cont+=cad_0.remove(cad_0.size()-1);
			}
		}else cont = max;
		System.out.println(cont);
	}

}


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

public class Main8 {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		ArrayList<Integer> cad_0 = new ArrayList<>();
		int cont = 0;
		int repete = 0;
		int max = 0;
		for (int i = 0; i < n; i++) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			if( b == 0 ){
				if(max<a)
					max = a;
				cad_0.add(a);
				continue;
			}else{
				cont += a;
				repete += b;
			}
		}
		if(repete!=0){
			int[] cad_1 = new int[cad_0.size()];
			int asdasd = cad_0.size();
			for (int i = 0; i < asdasd; i++)
				cad_1[i] = cad_0.remove(0);
			Arrays.sort(cad_1);
			for (int i = 0; i < cad_1.length; i++)
				cad_0.add(cad_1[i]);
			for (int i = repete-1; i >=0; i--) {
				cont+=cad_0.remove(cad_0.size()-1);
			}
		}else cont = max;
		System.out.println(cont);
	}

}


//package practice;
import java.util.*;

public class CF109B {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int loop=sc.nextInt();
		node[] data=new node[loop];
		for(int i=0;i<loop;i++)
		{
			data[i]=new node(sc.nextInt(),sc.nextInt());
		}
		Arrays.sort(data);
		int count=0;
		int ch=1;
		for(int i=loop-1;ch>0&&i>-1;i--)
		{
			ch--;
			ch+=data[i].b;
			count+=data[i].a;
		}
		System.out.println(count);
	}

	public static class node implements Comparable<node>
	{
		int a,b;
		public node(int aa,int bb)
		{
			a=aa;
			b=bb;
		}
		public int compareTo(node s)
		{
			if(b==s.b)
				return a-s.a;
			else
				return b-s.b;

		}
	}

}


import java.util.List;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author saket
 */
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);
		Combinations solver = new Combinations();
		solver.solve(1, in, out);
		out.close();
	}
}

class Combinations {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n=in.readInt();
        List<Point> choose=new ArrayList<Point>();
        List<Point> sorted=new ArrayList<Point>();
        for(int i=0;i<n;i++)
        {
            choose.add(new Point(in.readInt(), in.readInt()));
        }
        Collections.sort(choose,new Comparator<Point>() {
            public int compare(Point o1, Point o2) {
                if(o1.y>o2.y)
                    return -1;
                else if(o1.y==o2.y)
                    return -Integer.compare(o1.x,o2.x);
                else return 1;
            }
        });
        int b=1;
        int a=0;
        while(b>0&&choose.size()!=0)
        {
            Point p=choose.get(0);
            b+=p.y;
            a+=p.x;
            choose.remove(0);
            b--;
        }
        out.printLine(a);
    }
    class Point
    {
        int x,y;
        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private 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 readInt() {
        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 boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

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

class OutputWriter {
    private final PrintWriter writer;

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

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

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

}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<ctime>
#include<vector>

#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define ls (t<<1)
#define rs ((t<<1)|1)
#define mid ((l+r)>>1)

#define N 100005

using namespace std;

int h,n,i,j,k,p,m,Max,Maxid,CMax,CMaxid;

map<int,int>mp;

int Rand()
{
		return (rand()<<15)+rand();
}

int main()
{
	srand((int)time(0));
	scanf("%d",&h); n=(1<<h)-1;
	for (i=1;i<=420;++i)
	{
			int x=Rand()%n+1,y=Rand()%n+1,z=Rand()%n+1;
			while (y==x) y=Rand()%n+1;
			while (z==x||z==y) z=Rand()%n+1;
			printf("? %d %d %d\n",x,y,z);
			fflush(stdout);
			scanf("%d",&x);
			mp[x]++;
	}
	for (map<int,int>::iterator it=mp.begin();it!=mp.end();++it)
	{
			int x=it->fi,y=it->se;
			if (y>Max) swap(Maxid,CMaxid),swap(Max,CMax),Maxid=x,Max=y;
			else if (y>CMax) CMaxid=x,CMax=y;
	}
	for (i=1;i<=n;++i)
		if (i!=CMaxid&&i!=Maxid)
		{
				printf("? %d %d %d\n",CMaxid,Maxid,i);
				fflush(stdout);
				scanf("%d",&p);
				if (p==i)
				{
					printf("! %d\n",i);
					fflush(stdout);
					break;
				}
		}
}


#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

mt19937 rnd(time(0));
int h,n,cnt[1<<18];

signed main(){
	read(h);
	n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		int x,y,z;
		do{
			x=rnd()%n+1;
			y=rnd()%n+1;
			z=rnd()%n+1;
		}while(x==y||y==z||x==y);
		printf("? %d %d %d",x,y,z);cout<<endl;
		cnt[read(x)]++;
	}
	int x=0,y=0;
	for(int i=1;i<=n;i++){
		if(!x||cnt[i]>cnt[x]) y=x,x=i;
		else if(!y||cnt[i]>cnt[y]) y=i;
	}
	for(int i=1,r;i<=n;i++) if(i!=x&&i!=y){
		printf("? %d %d %d",i,x,y);
		if(read(r)==i) return printf("! %d",r),cout<<endl,0;
	}
}


#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

mt19937 rnd(time(0));
int h,n,cnt[1<<18];

signed main(){
	read(h);
	n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		int x,y,z;
		do{
			x=rnd()%n+1;
			y=rnd()%n+1;
			z=rnd()%n+1;
		}while(x==y||y==z||x==y);
		printf("? %d %d %d",x,y,z);cout<<endl;
		cnt[read(x)]++;
	}
	int x=0,y=0;
	for(int i=1;i<=n;i++){
		if(!x||cnt[i]>cnt[x]) y=x,x=i;
		else if(!y||cnt[i]>cnt[y]) y=i;
	}
	for(int i=1,r;i<=n;i++) if(i!=x&&i!=y){
		printf("? %d %d %d",i,x,y);cout<<endl;
		if(read(r)==i) return printf("! %d",r),cout<<endl,0;
	}
}


#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

mt19937 rnd(time(0));
int h,n,cnt[1<<18];

signed main(){
	read(h);
	n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		int x,y,z;
		do{
			x=rnd()%n+1;
			y=rnd()%n+1;
			z=rnd()%n+1;
		}while(x==y||y==z||x==z);
		printf("? %d %d %d",x,y,z);cout<<endl;
		cnt[read(x)]++;
	}
	int x=0,y=0;
	for(int i=1;i<=n;i++){
		if(!x||cnt[i]>cnt[x]) y=x,x=i;
		else if(!y||cnt[i]>cnt[y]) y=i;
	}
	for(int i=1,r;i<=n;i++) if(i!=x&&i!=y){
		printf("? %d %d %d",i,x,y);cout<<endl;
		if(read(r)==i) return printf("! %d",r),cout<<endl,0;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 2000005
#define inf 0x3f3f3f3f
#define LL long long
#define mod 1000000007
#define local
void file(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
template <typename Tp>void read(Tp &x){
	x=0;int fh=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-'){fh=-1;}c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}x*=fh;
}
int n,m;
mt19937 rnd(time(0));
int hh[maxn];
struct node{
	int id,vl;
	bool operator <(node y)const{
		return vl>y.vl;
	}
}a[maxn];
signed main(){
	read(m);n=((1<<m)-1);
	for(int i=1;i<=420;++i){
		int x=0,y=0,z=0,t;
		while(x==y||y==z||x==z){
			x=rnd()%n+1,y=rnd()%n+1,z=rnd()%n+1;
		}
		printf("? %d %d %d\n",x,y,z);fflush(stdout);
		scanf("%d",&t);
		hh[t]++;
	}
	for(int i=1;i<=n;++i)a[i]=(node){i,hh[i]};
	sort(a+1,a+n+1);
	int X=a[1].id,Y=a[2].id;
	for(int i=1;i<=n;++i){
		int t;
		printf("? %d %d %d\n",X,Y,i);fflush(stdout);
		scanf("%d",&t);
		if(t==i){
			printf("! %d\n",i);
			break;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 2000005
#define inf 0x3f3f3f3f
#define LL long long
#define mod 1000000007
#define local
void file(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
template <typename Tp>void read(Tp &x){
	x=0;int fh=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-'){fh=-1;}c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}x*=fh;
}
int n,m;
mt19937 rnd(time(0));
int hh[maxn];
struct node{
	int id,vl;
	bool operator <(node y)const{
		return vl>y.vl;
	}
}a[maxn];
signed main(){
	read(m);n=((1<<m)-1);
	for(int i=1;i<=420;++i){
		int x=0,y=0,z=0,t;
		while(x==y||y==z||x==z){
			x=rnd()%n+1,y=rnd()%n+1,z=rnd()%n+1;
		}
		printf("? %d %d %d\n",x,y,z);fflush(stdout);
		scanf("%d",&t);
		hh[t]++;
	}
	for(int i=1;i<=n;++i)a[i]=(node){i,hh[i]};
	sort(a+1,a+n+1);
	int X=a[1].id,Y=a[2].id;
	for(int i=1;i<=n;++i){
		if(i==X||i==Y)continue;
		int t;
		printf("? %d %d %d\n",X,Y,i);fflush(stdout);
		scanf("%d",&t);
		if(t==i){
			printf("! %d\n",i);
			break;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

mt19937 rnd(time(0));
int random()
{
	return rnd()%m+1;
//	return 1ll*rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d\n",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return 1ll*rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d\n",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return(long long) rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d\n",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return(long long) rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=n;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				fflush(stdout);
				return 0;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return(long long) rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d\n",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				fflush(stdout);
				return 0;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

mt19937 rnd(time(0));
int random()
{
//	return rnd()%m+1;
	return 1ll*rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(82420520);
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d\n",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return(long long) rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=n;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("%d\n",i);
				return 0;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return(long long) rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=n;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				return 0;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return ((rand()<<15)|rand())%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(82420520);
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d\n",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				break;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

int random()
{
	return(long long) rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(unsigned(time(0)));
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				fflush(stdout);
				return 0;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n,m;
struct qwq
{
	int id,val;
}hah[300010];

mt19937 rnd(time(0));
int random()
{
//	return rnd()%m+1;
	return 1ll*rand()*rand()%m+1;
}

bool cmp(qwq x,qwq y)
{
	return x.val>y.val;
}

int main()
{
	srand(19260817);
	scanf("%d",&n);
	m=(1<<n)-1;
	for(int i=1;i<=m;i++) hah[i].id=i;
	for(int i=1;i<=420;i++)
	{
		int x=random(),y=random(),z=random();
		while(x==y||y==z||x==z) x=random(),y=random(),z=random();
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		int lca;
		scanf("%d",&lca);
		hah[lca].val++;
	}
	sort(hah+1,hah+m+1,cmp);
	int X=hah[1].id,Y=hah[2].id;
	for(int i=1;i<=m;i++)
	{
		if(i!=X&&i!=Y)
		{
			printf("? %d %d %d\n",X,Y,i);
			fflush(stdout);
			int lca;
			scanf("%d",&lca);
			if(lca==i)
			{
				printf("! %d\n",i);
				break;
			}
		}
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(26);
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 420; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] < cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] < cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(48);
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 422; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] < cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] < cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(time(0));
int h, n, target[2];
struct pack {
  int id, cnt;
  friend bool operator<(pack x, pack y) {
    return x.cnt > y.cnt;
  }
} info[MAXN];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1; i <= n; i++) {
    info[i].id = i;
  }
  for (int i = 1, u, v, w; i <= 422; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    info[Query(u, v, w)].cnt++;
  }
  std::sort(info + 1, info + 1 + n);
  target[0] = info[1].id;
  target[1] = info[2].id;
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 20
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(time(0));
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 420; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] < cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] < cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(48);
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 420; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] < cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] < cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(26);
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 422; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] < cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] < cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 20
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(time(0));
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 422; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] < cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] < cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(34);
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 422; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] < cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] < cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(48);
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 420; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] <= cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] <= cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include <algorithm>
#include <cstdio>
#include <random>
#include <ctime>

// Definitions
#define MAXN 300010
#define MAXH 22
#define elif else if
#define File(_s) freopen(#_s ".in", "r", stdin), freopen(#_s ".out", "w", stdout)
typedef long long int64;

// Variables
std::mt19937 MyRand(32);
int h, n, cnt[MAXN], target[2];

// Main Functions
inline int Query(int u, int v, int w) {
  int res;
  printf("? %d %d %d\n", u, v, w);
  fflush(stdout);
  scanf("%d", &res);
  return res;
}
inline void Chat() {
  for (int i = 1, u, v, w; i <= 422; i++) {
    do { u = (MyRand() % n) + 1; } while (false);
    do { v = (MyRand() % n) + 1; } while (v == u);
    do { w = (MyRand() % n) + 1; } while (w == u || w == v);
    cnt[Query(u, v, w)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[target[0]] <= cnt[i]) {
      target[0] = target[1];
      target[1] = i;
    } elif (cnt[target[1]] <= cnt[i]) {
      target[1] = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (i == target[0] || i == target[1]) {
      continue;
    }
    if (Query(target[0], target[1], i) == i) {
      printf("! %d\n", i);
      fflush(stdout);
      break;
    }
  }
}
inline void Gather() {
  scanf("%d", &h);
  n = (1 << h) - 1;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
  File(data);
#endif
  Gather();
  Chat();
  return 0;
}
/*

*/


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+10;
int h,n,rt;
int cnt[N],id[N];
inline bool cmp(int x,int y){ return cnt[x]>cnt[y]; }
int rnd(int l,int r){
	ll x=1ll*rand()*rand()%(r-l+1);
	return l+x;
}
int ask(int x,int y,int z){
	printf("? %d %d %d\n",x,y,z);
	fflush(stdout);
	scanf("%d",&x); return x;
}
int main(){
	srand(time(NULL));
	scanf("%d",&h); n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		int u=rnd(1,n),v=rnd(1,n),w=rnd(1,n);
		if(u==v || v==w || u==w){ i--; continue; }
		cnt[ask(u,v,w)]++;
	}
	for(int i=1;i<=n;i++) id[i]=i;
	sort(id+1,id+n+1,cmp);
	int nd1=id[1],nd2=id[2];
	for(int i=1;i<=n;i++){
		if(i==nd1 || i==nd2) continue;
		if(ask(nd1,nd2,i)==i){ rt=i; break; }
	}
	printf("! %d\n",rt); fflush(stdout);
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+10;
int h,n,rt;
int cnt[N],id[N];
inline bool cmp(int x,int y){ return cnt[x]>cnt[y]; }
int rnd(int l,int r){
	int x=rand()%(r-l+1);
	return l+x;
}
int ask(int x,int y,int z){
	printf("? %d %d %d\n",x,y,z);
	fflush(stdout);
	scanf("%d",&x); return x;
}
int main(){
	scanf("%d",&h); n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		int u=rnd(1,n),v=rnd(1,n),w=rnd(1,n);
		if(u==v || v==w || u==w){ i--; continue; }
		int tp=ask(u,v,w);
		if(tp!=u && tp!=v && tp!=w) cnt[tp]++;
	}
	for(int i=1;i<=n;i++) id[i]=i;
	sort(id+1,id+n+1,cmp);
	int nd1=id[1],nd2=id[2];
	for(int i=1;i<=n;i++){
		if(i==nd1 || i==nd2) continue;
		if(ask(nd1,nd2,i)==i){ rt=i; break; }
	}
	printf("! %d\n",rt); fflush(stdout);
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+10;
int h,n,rt;
int cnt[N],id[N];
inline bool cmp(int x,int y){ return cnt[x]>cnt[y]; }
int rnd(int l,int r){
	ll x=1ll*rand()*rand()%(r-l+1);
	return l+x;
}
int ask(int x,int y,int z){
	printf("? %d %d %d\n",x,y,z);
	fflush(stdout);
	scanf("%d",&x); return x;
}
int main(){
	srand(time(NULL));
	scanf("%d",&h); n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		int u=rnd(1,n),v=rnd(1,n),w=rnd(1,n);
		if(u==v || v==w || u==w){ i--; continue; }
		int tp=ask(u,v,w);
		if(tp!=u && tp!=v && tp!=w) cnt[tp]++;
	}
	for(int i=1;i<=n;i++) id[i]=i;
	sort(id+1,id+n+1,cmp);
	int nd1=id[1],nd2=id[2];
	for(int i=1;i<=n;i++){
		if(i==nd1 || i==nd2) continue;
		if(ask(nd1,nd2,i)==i){ rt=i; break; }
	}
	printf("! %d\n",rt); fflush(stdout);
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+10;
int h,n,rt;
int cnt[N],id[N];
inline bool cmp(int x,int y){ return cnt[x]>cnt[y]; }
int rnd(int l,int r){
	ll x=1ll*rand()*rand()%(r-l+1);
	return l+x;
}
int ask(int x,int y,int z){
	printf("? %d %d %d\n",x,y,z);
	fflush(stdout);
	scanf("%d",&x); return x;
}
int main(){
	scanf("%d",&h); n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		int u=rnd(1,n),v=rnd(1,n),w=rnd(1,n);
		if(u==v || v==w || u==w){ i--; continue; }
		int tp=ask(u,v,w);
		if(tp!=u && tp!=v && tp!=w) cnt[tp]++;
	}
	for(int i=1;i<=n;i++) id[i]=i;
	sort(id+1,id+n+1,cmp);
	int nd1=id[1],nd2=id[2];
	for(int i=1;i<=n;i++){
		if(i==nd1 || i==nd2) continue;
		if(ask(nd1,nd2,i)==i){ rt=i; break; }
	}
	printf("! %d\n",rt); fflush(stdout);
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int h,n,t1,t2,t3,temp[1000002];
int t[1000002];
inline bool cmp(int x,int y){
	return t[x]>t[y];
}
int main(){
	srand(time(0));
    cin>>h;
    n=(1<<h)-1;
    F(i,1,420){
    	t1=rand()%n+1;
    	while((t2=rand()%n+1)==t1);
    	while((t3=rand()%n+1)==t1||t3==t2);
    	printf("? %d %d %d\n",t1,t2,t3);
    	fflush(stdout);
    	cin>>t1;
    	t[t1]++;
	}F(i,1,n)temp[i]=i;
	sort(temp+1,temp+n+1,cmp);
	F(i,3,n){
		printf("? %d %d %d\n",temp[1],temp[2],temp[i]);
		fflush(stdout);
		cin>>t1;
		if(t1==i){
			cout<<'!'<<' '<<i<<endl;
			return 0;
		}
	}
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int h,n,t1,t2,t3,temp[1000002];
int t[1000002];
inline bool cmp(int x,int y){
	return t[x]>t[y];
}
int main(){
	srand(time(0));
    cin>>h;
    n=(1<<h)-1;
    F(i,1,420){
    	t1=rand()%n+1;
    	while((t2=rand()%n+1)==t1);
    	while((t3=rand()%n+1)==t1||t3==t2);
    	printf("? %d %d %d\n",t1,t2,t3);
    	fflush(stdout);
    	cin>>t1;
    	t[t1]++;
	}F(i,1,n)temp[i]=i;
	sort(temp+1,temp+n+1,cmp);
	F(i,3,n){
		printf("? %d %d %d\n",temp[1],temp[2],temp[i]);
		fflush(stdout);
		cin>>t1;
		if(t1==temp[i]){
			cout<<'!'<<' '<<temp[i]<<endl;
			return 0;
		}
	}
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int w[1000002],l[1000002],ps[1000002],h,n,r,t1,t2,mmax,mpos;
int main(){
	srand(998244353);
    cin>>h;
    n=(1<<h)-1;
    F(i,1,n+420){
    	while(ps[r=(rand()+rand()*100)%n+1]){

		}while((t1=(rand()+rand()*100)%n+1)==r);
		while((t2=(rand()+rand()*100)%n+1)==r);
		printf("? %d %d %d\n",t1,t2,r);
		fflush(stdout);cin>>t1;
		if((t1)==r)w[r]++;
		else{
			l[r]++;
			ps[t1]=1;

		}
	}F(i,1,n){
		if(ps[i])continue;
		if(w[i]>=mmax){
			mpos=i;
			mmax=w[i];
		}
	}cout<<'!'<<' '<<mpos;
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int w[1000002],l[1000002],ps[1000002],h,n,r,t1,t2,mmax,mpos;
int main(){
	srand(998244353);
    cin>>h;
    n=(1<<h)-1;
    F(i,1,n+420){
    	while(ps[r=(rand()+rand()*100)%n+1]){

		}while((t1=(rand()+rand()*100)%n+1)==r);
		while((t2=(rand()+rand()*100)%n+1)==r||t2==t1);
		printf("? %d %d %d\n",t1,t2,r);
		fflush(stdout);cin>>t1;
		if((t1)==r)w[r]++;
		else{
			l[r]++;
			ps[t1]=1;

		}
	}F(i,1,n){
		if(ps[i])continue;
		if(w[i]>=mmax){
			mpos=i;
			mmax=w[i];
		}
	}cout<<'!'<<' '<<mpos;
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int h,n,t1,t2,t3,temp[1000002];
int t[1000002];
inline bool cmp(int x,int y){
	return t[x]>t[y];
}
int main(){
    cin>>h;
    n=(1<<h)-1;
    F(i,1,420){
    	t1=rand()%n+1;
    	while((t2=rand()%n+1)==t1);
    	while((t3=rand()%n+1)==t1||t3==t2);
    	printf("? %d %d %d\n",t1,t2,t3);
    	fflush(stdout);
    	cin>>t1;
    	t[t1]++;
	}F(i,1,n)temp[i]=i;
	sort(temp+1,temp+n+1,cmp);
	F(i,3,n){
		printf("? %d %d %d\n",temp[1],temp[2],temp[i]);
		fflush(stdout);
		cin>>t1;
		if(t1==i){
			cout<<'!'<<' '<<i<<endl;
			return 0;
		}
	}
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int w[1000002],l[1000002],ps[1000002],h,n,r,t1,t2,mmax,mpos;
int main(){
	srand(998244353);
    cin>>h;
    n=(1<<h)-1;
    F(i,1,n+420){
    	while(ps[r=(rand()+rand()*100)%n+1]){

		}while((t1=(rand()+rand()*100)%n+1)==r);
		while((t2=(rand()+rand()*100)%n+1)==r||t2==t1);
		printf("? %d %d %d\n",t1,t2,r);
		fflush(stdout);cin>>t1;
		if((t1)==r)w[r]++;
		else{
			l[r]++;
			ps[t1]=1;

		}
	}F(i,1,n){
		if(ps[i])continue;
		if(w[i]/max(l[i],1)>=mmax){
			mpos=i;
			mmax=w[i]/max(l[i],1);
		}
	}cout<<'!'<<' '<<mpos;
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int w[1000002],l[1000002],ps[1000002],h,n,r,t1,t2,mmax,mpos;
int main(){
	srand(998244353);
    cin>>h;
    n=(1<<h)-1;
    F(i,1,n+420){
    	while(ps[r=(rand()+rand()*100)%n+1]){

		}while((t1=(rand()+rand()*100)%n+1)==r);
		while((t2=(rand()+rand()*100)%n+1)==r);
		printf("? %d %d %d\n",t1,t2,r);
		fflush(stdout);
		if((t1=read())==r)w[r]++;
		else{
			l[r]++;
			ps[t1]=1;

		}
	}F(i,1,n){
		if(ps[i])continue;
		if(w[i]>=mmax){
			mpos=i;
			mmax=w[i];
		}
	}cout<<mpos;
    return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int w[1000002],l[1000002],ps[1000002],h,n,r,t1,t2,mmax,mpos;
int main(){
	srand(998244353);
    cin>>h;
    n=(1<<h)-1;
    F(i,1,n+420){
    	while(ps[r=(rand()+rand()*100)%n+1]){

		}while((t1=(rand()+rand()*100)%n+1)==r);
		while((t2=(rand()+rand()*100)%n+1)==r);
		printf("? %d %d %d\n",t1,t2,r);
		fflush(stdout);
		if((t1=read())==r)w[r]++;
		else{
			l[r]++;
			ps[t1]=1;

		}
	}F(i,1,n){
		if(ps[i])continue;
		if(w[i]>=mmax){
			mpos=i;
			mmax=w[i];
		}
	}cout<<'!'<<' '<<mpos;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int cnt[1000005];

int main()
{
	int h,n,x,y,z;
	scanf("%d",&h);
	n=(1<<h)-1;
	for(int i=1;i<=420;i++){
		x=rand()%n;
		do{
			y=rand()%n;
		}while(y==x);
		do{
			z=rand()%n;
		}while(z==y||z==x);
		printf("? %d %d %d\n",x+1,y+1,z+1);
		fflush(stdout);
		scanf("%d",&x); cnt[x]++;
	}
	int id1=0,id2=0;
	for(int i=1;i<=n;i++){
		if(cnt[i]>cnt[id1])id2=id1,id1=i;
		else if(cnt[i]>cnt[id2])id2=i;
	}
	for(int i=1;i<=n;i++){
		if(i==id1||i==id2)continue;
		printf("? %d %d %d\n",id1,id2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x==i){
			printf("! %d\n",x);
			fflush(stdout);
			break;
		}
	}
	return 0;
}


#include <cstdio>
#include <map>
using std::map;
using std::pair;

int read(){
	int x =0; char c =getchar(); bool f =0;
	while(c < '0' || c > '9') (c == '-') ? f =1, c =getchar() : c =getchar();
	while(c >= '0' && c <= '9') x =(x<<1)+(x<<3)+(48^c), c =getchar();
	return (f) ? -x : x;
}

inline int Ask(const int &u, const int &v, const int &w){
	printf("? %d %d %d\n", u, v, w);
	fflush(stdout);
	return read();
}

map<int, int> Map, Map2;

int main(){
	srand(19260817);
	int h =read();
	int n =(1<<h)-1;
	for(int i =0; i < 420; ++i){
		int u =((rand()<<16)|rand())%n+1, v =((rand()<<16)|rand())%n+1, w =((rand()<<16)|rand())%n+1;
		while(v == u)
			v =(v-1 + 1)%n+1;
		while(w == u || w == v)
			w =(w-1 + 1)%n+1;
		int lca =Ask(u, v, w);
		if(!Map.count(lca))
			Map.insert(pair<int, int>(lca, 1));
		else
			++Map.find(lca)->second;
	}
	for(pair<int, int>pad:Map)
		Map2.insert(pair<int, int>(pad.second, pad.first));
	int son1 =(--Map2.end())->second, son2 =(--(--Map2.end()))->second;
	for(int i =1; i <= n; ++i)
		if(i != son1 && i != son2)
			if(Ask(son1, son2, i) == i){
				printf("! %d", i);
				return 0;
			}
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=422;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	//printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
}


#include<bits/stdc++.h>
#define R (rand()|181766&5156951569^5156918176)%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0)); srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=415;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=n;i;i--) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=n;i;i--) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	//printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
	printf("! %d",rand()%n+1);
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=422;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) break;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=422;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=10;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0)); srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=415;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=n;i;i--) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	//printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
	printf("! %d",rand()%n+1);
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0)); srand((unsigned int) time(0));
	register int i;printf("! 1");
	/*scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=300;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	//printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}*/
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0)); srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=415;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	//printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
	printf("! %d",rand()%n+1);
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0)); srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=400;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	//printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
}


#include<bits/stdc++.h>
#define R rand()*rand()%n+1
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m,k,x,y,z,f[1000039],maxn1,maxn2;
int main(){
	srand((unsigned int) time(0));
	register int i;
	scanf("%d",&m);
	n=(1<<m)-1;
	for(i=1;i<=422;i++){
		x=R;y=R;z=R;
		while(y==x) y=R;
		while(z==y||z==x) z=R;
		printf("? %d %d %d\n",x,y,z);
		fflush(stdout);
		scanf("%d",&x);
		f[x]++;
	}
	for(i=1;i<=n;i++)maxn1=max(maxn1,f[i]);
	for(i=1;i<=n;i++) if(maxn1==f[i]){f[i]=0;maxn1=i;break;}
	for(i=1;i<=n;i++) maxn2=max(maxn2,f[i]);
	for(i=1;i<=n;i++) if(f[i]==maxn2){f[i]=0;maxn2=i;break;}
	//printf("%d %d\n",maxn1,maxn2);
	for(i=1;i<=n;i++){
		if(i==maxn1||i==maxn2) continue;
		printf("? %d %d %d\n",maxn1,maxn2,i);
		fflush(stdout);
		scanf("%d",&x);
		if(x!=maxn1&&x!=maxn2) {printf("! %d",i);return 0;}
	}
}////