1733A - Consecutive Sum - CodeForces Solution


greedy sortings

Please click on ads to support us..

Python Code:

import sys
input = lambda: sys.stdin.readline().rstrip()
in_il = lambda: list(map(int, input().split()))
in_sl = lambda: input().split()
in_i = lambda: int(input())
MAX_INT = float('inf')
 
def solve():
    n, k = in_il()
    arr = in_il()
    for i in range(k):
        for j in range(i+k, n, k):
            arr[i] = max(arr[i], arr[j])
    
    print(sum(arr[:k]))
 
for _ in range(in_i()):
    solve()

C++ Code:

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

int main()
{
  int t;
  cin >> t;
  while (t--)
  {
    long long n, k, j = 1;
    long long sum = 0;
    cin >> n >> k;
    map<int, int> m;
    map<int, int>::iterator it;
    for (int i = 1; i <= n; i++)
    {
      int x;
      cin >> x;
      if (m[j] < x)
        m[j] = x;

      j++;
      if (i % k == 0)
        j = 1;
    }
    for (it = m.begin(); it != m.end(); it++)
    {
      sum = sum + it->second;
    }
    cout << sum << endl;
  }
}


Comments

Submit
0 Comments
More Questions

1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush