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 Textbooks

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

Question

Codey, who is enthusiastic when it comes to studies, is eager to purchase n textbooks from the university bookstore. These textbooks are priced in an interesting way:

  • The first textbook costs mmm dollars.

  • The second textbook costs 2āˆ—m2 * m2āˆ—m dollars.

  • The iii-th textbook costs iāˆ—mi * miāˆ—m dollars.

Codey has k dollars in his wallet, but it's wondering how much it needs to borrow from the E-bee to cover the costs of purchasing n textbooks.

Your task is to calculate and output the amount of money that Codey has to borrow from the E-bee. If it doesn't need to borrow any money, the answer should be 000.

Can you assist Codey in figuring out how much it needs to borrow from E-Bee, if at all?

Input Format

The first line contains three integers n, m, k, where n represents the number of textbooks Codey wants, m represents the cost of the first book, and k represents the initial number of ringgit Codey has.

Constraints

0≤n,m,k≤1050 \le n, m, k \le 10^50≤n,m,k≤105

Output Format

Outputs only one integer, the amount of money Codey has to borrow from E-bee. If it doesn't have to borrow money from E-bee, output 0.

Sample Inputs:

Input

5 2 3

Output

27

Explanation

Codey wants 5 textbooks, and the first book costs 2 ringgit. The cost of the books is:

(1āˆ—2)+(2āˆ—2)+(3āˆ—2)+(4āˆ—2)+(5āˆ—2)=30(1 * 2) + (2 * 2) + (3 * 2) + (4 * 2) + (5 * 2) = 30(1āˆ—2)+(2āˆ—2)+(3āˆ—2)+(4āˆ—2)+(5āˆ—2)=30

Codey has 3 ringgit, therefore Codey needs to borrow 30 - 3 = 27 ringgit.

Input

3 2 15

Output

0

Explanation

Codey doesn't have to borrow money from E-bee, because Codey has sufficient money.


Solution - Sn AP

This is a typical "find the sum of AP" question. The formula of Sum of AP series is:n2āˆ—(2a+(nāˆ’1)āˆ—d)\frac{n}{2} * (2a + (n-1) * d)2nā€‹āˆ—(2a+(nāˆ’1)āˆ—d)

we know that d is always 1, so we can omit it. Substitute a to m, and we have the total price. Lastly, subtract k to see if it is more than 0. If it isn't, print 0; otherwise, print that value. The value is the amount of money that Codey has to borrow.

Here's the solution:

n, m, k = map(int, input().strip().split())
ans = ((n * (n + 1) // 2) * m) - k
if ans <= 0:
    print("0")
else:
    print(int(ans))
PreviousCodey and MathNextCodey and Money

Last updated 2 months ago

šŸ‡²šŸ‡¾