Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ktyu
Algorithm_Practice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Algorithm_Practice
/
2878.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
41 lines (27 loc) · 1.64 KB
master
Breadcrumbs
Algorithm_Practice
/
2878.java
Copy path
Top
File metadata and controls
Code
Blame
41 lines (27 loc) · 1.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(); // 가진 사탕의 수
int n = sc.nextInt(); // 친구 수
int[] arr = new int[n]; // 각자가 받고 싶은 사탕 개수
long sum = -1*(long)m; // 줄 수 없는 사탕 개수들의 합
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
sum += (long)arr[i];
}
// MAIN Point -> 숫자들의 합과 개수가 같을 때에는 숫자들이 서로 비슷해야 제곱의 합이 작아짐
// -> ex) 4^2 + 4^2 (32)< 1^2 + 7^2 (50)
// 전략 -> 못준 사탕의 개수들의 합을 인원수에 맞게 균일하게 나눔
long answer = 0; // 제곱의 합을 더할 변수
// (2^64로 나눈다는건 ~(1 << 64)와 & 연산을 하는것과 동일하므로 long 타입을 쓰고 신경안써도 되는 조건임)
Arrays.sort(arr); // 큰 수가 조금이라도 더 받아야하므로 뒤쪽에서 처리되어야함
for(int i=0; i<n; i++) {
long fair = sum / (n-i); // 아직 못준 개수를 인원수만큼 균등하게 분배 (그리디 알고리즘 == 현재 순간의 최적해를 선택)
long val = Math.min((long)arr[i], fair); // 받아야할 개수가 균등분배의 양보다 적으면, 하나도 주지 않음
answer += val*val; // i번째 친구에게 못준 개수의 제곱
sum -= val; // i번째 친구에게 못준 사탕의 개수 제거
}
System.out.println(answer);
}
}
You can’t perform that action at this time.