Beats 90% - Most easy way on INTERNET to understand (C#)
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```csharp []
public class Solution {
    public int[] FindXSum(int[] nums, int k, int x) {

        List<int> ls = new List<int>();
        Dictionary<int,int> dic = new Dictionary<int,int>();

        for(int i = 0, j=0; j<nums.Length ; j++)
        {
            if(dic.ContainsKey(nums[j]))
            {
                dic[nums[j]]++;
            }
            else
            {
                dic.Add(nums[j],1);
            }

            //calculate window
            if(((j+1)-i)==k)
            {
               ls.Add(Calculate(dic, x));

                //First element removal
                if(dic[nums[i]] > 1)
                {
                    dic[nums[i]]--;
                }
                else
                {
                    dic.Remove(nums[i]);
                }
                 //shift window ahead from left
                i++;
            }

           
         
        }  


        return ls.ToArray();
    } 

    public int Calculate(Dictionary<int,int> dic, int x)
    {
        int result = 0;

         if(dic.Count<x)
        {
            foreach(var d in dic )
            {
                result = result + (d.Key * d.Value);
            }

            return result;
        }


        PriorityQueue<int,(int,int)> queue = new PriorityQueue<int,(int,int)>(
            Comparer<(int, int)>.Create((a, b) =>
            {
                // Higher occurrence first
                if (a.Item1 != b.Item1) return b.Item1.CompareTo(a.Item1);          
            // Higher value in case of tie
                return b.Item2.CompareTo(a.Item2); 
            })
        );


        foreach(var d in dic )
        {
            queue.Enqueue(d.Key,(d.Value,d.Key));
        }
        
        for(int i = 0; i < x; i++)
        {
            int xx = queue.Dequeue();
            result = result + (dic[xx] * xx);
        }

        return result;

    }



}

   

```
['C#']