#include using namespace std; typedef long long ll; int gcd(int a, int b){ while(b != 0){ int t = b; b = a % b; a = t; } return a; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, k, p, q; cin >> n >> k >> p >> q; vector arr; vector duparr(2*n); for(int i = 0; i < n; i++){ int tmp; cin >> tmp; arr.push_back(tmp); duparr[i] = arr[i]; duparr[i+n] = arr[i]; } vector pSubrows(n); ll currsum = 0; for(int i = 0; i < n+q-1; i++){ currsum+=duparr[i]; if(i >= q-1){ pSubrows[i-q+1] = currsum; currsum-=duparr[i-q+1]; } } // this will store the index of the top-left element of the subrectangle int buckets = gcd(n,k); int bucketLen = n/buckets; vector> subrows(buckets, vector(bucketLen)); vector> sumToIndex(buckets, vector(bucketLen)); for(int bucket = 0; bucket < buckets; bucket++){ int idx = bucket; for(int i = 0; i < bucketLen; i++){ subrows[bucket][i] = pSubrows[idx]; sumToIndex[bucket][i] = idx; idx+=k; idx%=n; } } vector> subrowSums(buckets, vector(bucketLen)); for(int bucket = 0; bucket < buckets; bucket++){ long totsum = 0; for(int i = 0; i < bucketLen; i++) totsum+=subrows[bucket][i]; int mult = p/bucketLen; int extra = p%bucketLen; long currsum2= totsum*mult; for(int i = 0; i < bucketLen+extra; i++){ if(i >= extra){ subrowSums[bucket][(i-extra)%bucketLen] = currsum2; currsum2-=subrows[bucket][(i-extra)%bucketLen]; } currsum2+=subrows[bucket][i%bucketLen]; } } // now we check which subrow sums actually exist in the grid bool all = false; vector markers(2*n, 0); int start = 0; for(int i = 0; i < n-p+1; i++){ int end = (start+(n-q+1)); if(end-start >= n){ all = true; break; } markers[start]++; markers[end]--; start+=k; start%=n; } vector exists; int startVal = 0; for(int i = 0; i < 2*n; i++){ startVal+=markers[i]; exists.push_back(startVal); } vector elements(n, false); for(int i = 0; i < n; i++){ if(exists[i] != 0 || exists[i+n] != 0){ elements[i] = true; } } ll ans = LONG_MIN; for(int i = 0; i < buckets; i++){ for(int j = 0; j < bucketLen; j++){ if(all || elements[sumToIndex[i][j]]) ans = max(ans, subrowSums[i][j]); } } cout << ans << endl; }