EWTD

Untitled

Oct 18th, 2020
298
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define max(a,b) \
  4.    ({ __typeof__ (a) _a = (a); \
  5.        __typeof__ (b) _b = (b); \
  6.      _a > _b ? _a : _b; })
  7.  
  8. int main(){
  9.     int n = 0, k = 0;
  10.     long max_sum = 0, current_sum = 0;
  11.     int index = 0;
  12.     long value;
  13.     scanf("%d%d",&n,&k);
  14.     long* arr = (long*)malloc(k*sizeof(long));
  15.     while(index < k && 1 == scanf("%ld",&arr[index])){
  16.         current_sum += arr[index];
  17.         index++;
  18.     }
  19.    
  20.     int ans = 0;
  21.     int arr_index = 0;
  22.     index = 1;
  23.     max_sum = current_sum;
  24.     while(1 == scanf("%ld",&value)){
  25.         current_sum = current_sum - arr[arr_index] + value;
  26.         arr[arr_index] = value;
  27.         if(current_sum > max_sum){
  28.             max_sum = current_sum;
  29.             ans = index;
  30.         }
  31.         arr_index = (arr_index+1)%k;
  32.         index++;
  33.     }
  34.     printf("%d",ans);
  35.     free(arr);
  36.     return 0;
  37. }
  38.  
RAW Paste Data