1. const int NMAX = 505;

  2. int N, K, a[NMAX], b[NMAX];

  3. bool dp[NMAX][NMAX];


  4. int main()

  5. {

  6. #ifndef ONLINE_JUDGE

  7. ifstream cin("test.in");

  8. #endif

  9. #ifdef ONLINE_JUDGE

  10. ios_base::sync_with_stdio(false);

  11. cin.tie(nullptr);

  12. #endif

  13. long long sum = 0, answer = 0;

  14. cin >> N >> K;

  15. for (int i = 1; i <= N; ++i)

  16. {

  17. cin >> a[i] >> b[i];

  18. sum += a[i] + b[i];

  19. }

  20. dp[0][0] = true;

  21. for (int i = 1; i <= N; ++i)

  22. {

  23. for (int j = 0; j < K; ++j)

  24. {

  25. dp[i][j] = dp[i - 1][(j - a[i] % K + K) % K];

  26. for (int l = 0; l <= min(K - 1, a[i]); ++l)

  27. {

  28. if ((a[i] - l) % K + b[i] >= K)

  29. dp[i][j] |= dp[i - 1][(j - l + K) % K];

  30. }

  31. }

  32. }


  33. for (int i = 0; i < K; ++i)

  34. if (dp[N][i] == true)

  35. answer = max(answer, (sum - i) / K);

  36. cout << answer << "\n";



  37. return 0;

  38. }