Where is likely the performance bug here? : <p>Many of the test cases are timing out. I've made sure I'm using lazy evaluation everywhere, linear (or better) routines, etc. I'm shocked that this is still not meeting the performance benchmarks. </p>

<pre><code>using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Mine
{
    public int Distance { get; set; } // from river
    public int Gold { get; set; } // in tons
}

class Solution 
{
    static void Main(String[] args) 
    {
        // helper function for reading lines
        Func&lt;string, int[]&gt; LineToIntArray = (line) =&gt; Array.ConvertAll(line.Split(' '), Int32.Parse);

        int[] line1 = LineToIntArray(Console.ReadLine());
        int N = line1[0], // # of mines
            K = line1[1]; // # of pickup locations

        // Populate mine info
        List&lt;Mine&gt; mines = new List&lt;Mine&gt;();
        for(int i = 0; i &lt; N; ++i)
        {
            int[] line = LineToIntArray(Console.ReadLine());
            mines.Add(new Mine() { Distance = line[0], Gold = line[1] });
        }

        // helper function for cost of a move
        Func&lt;Mine, Mine, int&gt; MoveCost = (mine1, mine2) =&gt; 
            Math.Abs(mine1.Distance - mine2.Distance) * mine1.Gold;

        int sum = 0; // running total of move costs

        // all move combinations on the current list of mines, 
        // given by the indicies of the mines
        var indices = Enumerable.Range(0, N); 
        var moves = from i1 in indices
                    from i2 in indices
                    where i1 != i2
                    select new int[] { i1, i2 };

        while(N != K) // while number of mines hasn't been consolidated to K
        {           
            // get move with the least cost
            var cheapest = moves.Aggregate(
                (prev, cur) =&gt; MoveCost(mines[prev[0]],mines[prev[1]]) 
                                   &lt; MoveCost(mines[cur[0]], mines[cur[1]])
                               ? prev : cur
            );

            int i = cheapest[0], // index of source mine of cheapest move
                j = cheapest[1]; // index of destination mine of cheapest move

            // add cost to total
            sum += MoveCost(mines[i], mines[j]);

            // move gold from source to destination          
            mines[j].Gold += mines[i].Gold;

            // remove from moves any that had the i-th mine as a destination or source
            moves = from move in moves
                    where move[0] == i || move[1] == i
                    select move;

            // update size number of mines after consolidation
            --N;    
        }

        Console.WriteLine(sum);
    }
}
</code></pre>
