using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace EDP_D
{
    class Program
    {
        class Answer
        {
            public long Max;
            public List<long> Selection;

            public Answer(long m, IEnumerable<long> s) { Max = m; Selection = s.ToList(); }
        }

        struct Item
        {
            public long Value { get; set; }
            public long Weight { get; set; }

            public Item(long v, long w)
            {
                Value = v;
                Weight = w;
            }
        }

        static Answer knapsack(long wsize, List<Item> items)
        {
            var n = items.Count();
            // [i][w] iまでのitemで、wを超えない範囲で最大の価値
            var c = new long[n + 1, wsize + 1];
            var g = new bool[n + 1, wsize + 1];

            foreach (var i in Enumerable.Range(1, n))
            {
                for (long w = 1; w <= wsize; w++)
                {
                    var prev = c[i - 1, w];
                    if (items[i - 1].Weight <= w)
                    {
                        // 入れられる
                        var current = c[i - 1, w - items[i - 1].Weight] + items[i - 1].Value;
                        if (current > prev)
                        {
                            c[i, w] = current;
                            g[i, w] = true;
                        }
                        else
                        {
                            c[i, w] = prev;
                            g[i, w] = false;
                        }
                    }
                    else
                    {
                        // 入れられないので、前回を引き継ぐ
                        c[i, w] = prev;
                        g[i, w] = false;
                    }
                }
            }

            var selection = new List<long>();

            var wt = wsize;
            foreach (var i in Enumerable.Range(1, n).Reverse())
            {
                if (g[i, wt])
                {
                    selection.Add(i);
                    wt -= items[i - 1].Weight;
                }
            }

            selection.Reverse();
            return new Answer(c[n, wsize], selection);
        }

        static void Solve()
        {
            var n = Input.NextInt();
            var wsize = Input.NextInt();
            var items = new List<Item>();

            foreach (var i in Enumerable.Range(0, n))
            {
                var w = Input.NextInt();
                var v = Input.NextInt();
                items.Add(new Item(v, w));
            }

            var ans = knapsack(wsize, items);

            Console.WriteLine(ans.Max);
        }

        #region Competitive Template
        public static void Main(string[] args)
        {
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 仮想的に標準入力をセットする
            // NextLine系を使っていると使えない
            //Input.SetText("");

            Solve();

            Console.Out.Flush();
        }

        static class Input
        {
            static char[] separator = { ' ' };
            public static bool IsEof { get; set; }
            static Queue<string> q { get; set; }

            static Input()
            {
                IsEof = false;
                q = new Queue<string>();
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            /// <param name="items"></param>
            public static void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                {
                    SetText(item);
                }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static bool SetText(string s)
            {
                if (s == null) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                {
                    q.Enqueue(elem);
                }

                return true;
            }

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            /// <returns></returns>
            static bool read()
            {
                var s = Console.ReadLine();
                if (s == null) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                {
                    q.Enqueue(elem);
                }

                if (!q.Any()) return read();

                return true;
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            /// <returns></returns>
            public static string Next()
            {
                if (!q.Any())
                {
                    if (!read())
                    {
                        IsEof = true;
                        return "";
                    }
                }

                return q.Dequeue();
            }

            public static int NextInt() => int.Parse(Next());
            public static long NextLong() => long.Parse(Next());
            public static double NextDouble() => double.Parse(Next());

            public static List<string> Next(int n) => Enumerable.Range(0, n).Select(_ => Next()).ToList();
            public static List<int> NextInt(int n) => Next(n).Select(x => int.Parse(x)).ToList();
            public static List<long> NextLong(int n) => Next(n).Select(x => long.Parse(x)).ToList();
            public static List<double> NextDouble(int n) => Next(n).Select(x => double.Parse(x)).ToList();

            public static List<string> NextLine() => Console.ReadLine().Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();
            public static List<int> NextIntLine() => NextLine().Select(x => int.Parse(x)).ToList();
            public static List<long> NextLongLine() => NextLine().Select(x => long.Parse(x)).ToList();
            public static List<double> NextDoubleLine() => NextLine().Select(x => double.Parse(x)).ToList();
        }
        #endregion
    }
}


Measured memory_bytes: 101080