EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2023
  3. Preliminary Round

Codey and Team Selection

https://www.hackerrank.com/contests/codenection-2023-preliminary-round-closed-category/challenges/cn-c6

PreviousCodey and MoneyNextCodey and Painted Tree

Last updated 2 months ago

Question

Codey is a coach, and it is assembling a team for a competitive sports league. It is going to select n players for the team. Each player possesses a skill point represented by aia_iai​. The player selection process consists of n steps, and in each step:

  • Let b to be the sorted array of skill points for unselected players. Choose an integer j, where 1≤j≤k1 \le j \le k1≤j≤k, and k is the current total of unselected players, that player joins the team and gains an additional skill point, sjs_jsj​. In other words, the total team skill increases by bj+sjb_j + s_jbj​+sj​.

Codey wants to strategically select players to maximize the overall team skill for the upcoming sports league, while following the constraints of the selection process. Can you help Codey to achieve this?

Input Format

The first line contains a single integer n, which represents the number of players.

The second line contains n integers a1,a2,...,ana_1, a_2, ..., a_na1​,a2​,...,an​, each representing the skill points of the iii-th player.

The third line contains n integers s1,s2,...,sns_1, s_2, ..., s_ns1​,s2​,...,sn​, each representing the additional skill points gained for selecting the jjj-th player in the array b.

Constraints

1≤n≤1051 \le n \le 10^51≤n≤105
1≤ai≤1051 \le a_i \le 10^51≤ai​≤105

Output Format

Output an integer representing the maximum total team skill.

Sample Inputs:

Input

3
1 2 3
3 3 1

Output

15

Explanation

We can select the players as such:


Solution - Question is Confusing

This question is so confusing that I couldn't finish at the competition QAQ

After been three days of reading, finally I get it how the coach selects the players. Here's the details:

  • Each player position affects how many skill points, sj, can be chosen.

    • For example, player[0] can only choose s[0], player[1] can choose between s[0] and s[1], and so on.

Which means, we had to do two loops which:

  1. loop through players and indexes

  2. find the maximum skill points that player can get based on the position of player.

But this will get a time limit exceeded, as the array is large enough to cough the program out of time.

One way I thought to optimize it is by memorization, which is to by memorize the current maximum value from that array, next time only need to compare that max with the next index cell.

And not surprising, memorization works and passed all test cases!

Here's my code:

t = int(input().strip())
arr1 = list(map(int, input().strip().split()))
arr2 = list(map(int, input().strip().split()))

max_cache = 0
total = 0

for i, x in enumerate(arr1):
    max_cache = max(max_cache, arr2[i])
    total += x + max_cache

print(total)
1≤sj≤10k1 \le s_j \le 10^k1≤sj​≤10k
1≤a1≤a2≤...≤an1 \le a_1 \le a_2 \le ... \le a_n1≤a1​≤a2​≤...≤an​

The current team skill is 000 and b = [1, 2, 3]. We choose j = 1 and the team skill increase by b1+s1=1+3=4b_1 + s_1 = 1 + 3 = 4b1​+s1​=1+3=4.

The current team skill is 0+4=40 + 4 = 40+4=4 and b = [2, 3]. We choose j = 2 and the team skill increase by b2+s2=3+3=6b_2 + s_2 = 3 + 3 = 6b2​+s2​=3+3=6.

The current team skill is 4+6=104 + 6 = 104+6=10 and b = [2]. We choose j = 1 and the team skill increase by b1+s1=2+3=5b_1 + s_1 = 2 + 3 = 5b1​+s1​=2+3=5.

All players are selected, selection ended. The total team skill is 0+4+6+5=150 + 4 + 6 + 5 = 150+4+6+5=15.

🇲🇾