fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. int arr[] = new int[n];
  15. for(int i=0;i<n;i++){
  16. arr[i]=sc.nextInt();
  17. }
  18. int max = 0;
  19. Map<Integer,Integer>mp=new HashMap<>();
  20. for(int i=0;i<n;i++){
  21. int temp = arr[i];
  22. int digsum = 0;
  23. while(temp>0){
  24. digsum=digsum+temp%10;
  25. temp=temp/10;
  26. }
  27. if(mp.containsKey(digsum)){
  28. int idx = mp.get(digsum);
  29. int val = arr[idx];
  30. if(val+arr[i]>max){
  31. max=val+arr[i];
  32. }
  33. }else{
  34. mp.put(digsum,i);
  35. }
  36. }
  37. System.out.println(max);
  38. }
  39. }
Success #stdin #stdout 0.12s 54504KB
stdin
4
51
71
17
42
stdout
93