Advertisement
Guest User

Untitled

a guest
Aug 29th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. int main()
  4. {
  5.     freopen("input.txt", "r", stdin);
  6.     freopen("output.txt", "w", stdout);
  7.     int N, M, piante[1000001];
  8.     int sx = 0, dx = -1;
  9.     int sum = 0;
  10.     int cont = 0;
  11.     scanf("%d %d", &M, &N);
  12.  
  13.     for(int i = 0; i < N; i++) scanf("%d", piante+i);
  14.  
  15.     while(dx < N - 1)
  16.     {
  17.     while(sum < M && dx < N -1) sum += piante[++dx];
  18.     while(sum == M && dx < N - 1)
  19.     {
  20.         int tSx = 1, tDx = 1;
  21.         while(piante[sx] == 0 && sx < dx) { tSx++; sx++; }
  22.         while(piante[dx+1] == 0 && dx < N-1) { tDx++; dx++; }
  23.         cont += (tSx*tDx);
  24.         sum -= piante[sx++];
  25.     }
  26.     while(sum > M && sx < dx) sum -= piante[sx++];
  27.     }
  28.  
  29.     printf("%d\n", cont);
  30.    
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement