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

int main() 
{
    int n;
    cin>>n; // 5

    int a[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i]; // 1 1 2 3 4
    }

    int k;
    cin>>k; // 3 

    int i=0 , j=0;         int sum = 0;       int ans = -1;

    unordered_map<int,int>mp;

    while(j<n)
    {
        //calculation part
        sum = sum + a[j]; // Running sum find out kela
        mp[a[j]] = mp[a[j]] + 1; // increment the map to count the frequency of each element 

        if(j-i+1 < k)
        {
            j++;
        }
        else if(j-i+1 == k)
        {
            // mp.size means map madhe jevdhe elements sarvanchi frequency same aahe... tar mg ti equal to k honar
            if(mp.size() == k)
            {
                ans = max(ans , sum); // get the maximum sum
            }

            // for a window mp.size() < k then it means duplicates are present in the window
            // and then whatever value is present in the sum can not be the potential ans

            // slide the window
            sum = sum - a[i]; // Remove that ' i ' index Element from sum it will decrease the sum
            mp[a[i]] = mp[a[i]] - 1; // also remove from map it will Decrease  the frequency from the map 

            // if frequency of that particular element becomes zero in map
            if(mp[a[i]] == 0)
            {
                mp.erase(a[i]); // Remove the element from map also 
            }

            // Increment both the pointers
            i++; 

            j++;
        }
    }
    cout<<ans<<" "; 
}